如何根据不同模型中的字段排除对象?

时间:2011-09-07 06:44:50

标签: django django-models

我有两种模式:

class Model1(models.Model):
    field1 = models.BooleanField()
    field2 = ...

class Model2(models.Model):
    field21 = ...
    field22 = models.ForeignKey(Model1)
    objects = FilterManager()

以及如何在FilterManager(Model1)中排除对象包含Model1.field1 = False?

我的FilterManager:

class FilterManager(models.Manager):

    def optfilter(self, options = dict()):
         if options.has_key('option'):
             kwargs['field21'] = options['option']

         return self.filter(**kwargs)

1 个答案:

答案 0 :(得分:2)

尝试这样的事情:

Model2.objects.exclude(field22__field1=False)