在Django中,有一种方法可以过滤许多为空或null的字段。
class TestModel(models.Model):
name = models.CharField(_('set name'), max_length=200)
manytomany = models.ManyToManyField('AnotherModel', blank=True, null=True)
print TestModel.objects.filter(manytomany__is_null=True)发布于 2010-11-15 21:04:19
print TestModel.objects.filter(manytomany=None)发布于 2022-02-24 10:22:37
尽管这个话题已经有了答案,但这可能会有所帮助。尝试查找:
empty = TestModel.objects.filter(manytomany__isnull = True)
#........If you want to get their counter part
not_empty = TestModel.objects.filter(manytomany__isnull = False)基本上,你会得到两个查询集:一个是你的许多字段都是空的,另一个是在许多字段中有数据的对象。
希望这能对你有所帮助!
https://stackoverflow.com/questions/4184558
复制相似问题