Django Model Manager Database Error

I am trying to create a custom model manager, but encountered an error. The code is as follows:

class LookupManager(models.Manager):
    def get_options(self, *args, **kwargs):
        return [(t.key, t.value) \
                for t in Lookup.objects.filter(group=args[0].upper())]


class Lookup(models.Model):
    group = models.CharField(max_length=1)
    key = models.CharField(max_length=1)
    value = models.CharField(max_length=128)
    objects = LookupManager()

(I quite often played with get_options, using super()other methods of filtering the results)

When I run syncdb, I get the following error ( ops_lookup- the corresponding table):

django.db.utils.DatabaseError: no such table: ops_lookup

I noticed that if I change the manager to return []instead of a filter, it works syncdb. In addition, if I run syncdband all the tables exist, then change the code to above, it also works.

How can I make Django not expect this table to exist on startup syncdbfor the first time?

, , . , , . , , , , , , .

django ( ?)

+3
1

, , . :

class MyModel(models.Model):
    name = models.CharField(max_length=255)


class OtherModel(models.Model):
    some_field = models.CharField(
        max_length=255,
        # Next line fails on syncdb because the database table hasn't been created yet
        # but the model is being queried during module load time (during class definition)
        choices=[(o.pk, o.name) for o in MyModel.objects.all()]
    )

, , , , (), .

, . , :

choices=((o.pk, o.name) for o in MyModel.objects.all())

, :

class LookupManager(models.Manager):
    def get_options(self, *args, **kwargs):
        return ((t.key, t.value) for t in Lookup.objects.filter(group=args[0].upper()))

( ( ) [ ]) () - .

0

All Articles