伙计们正试图在我的模型中的字段中过滤重复项
例如我的模型是:
class Household(models.Model):
office = models.ForeignKey(
Office,
related_name='households',
help_text=_('The office that manages this data'),
)
uuid = UUIDField(
auto=True,
version=4,
help_text=_('Unique identifier for each household'),
)
name = models.CharField(
_('Family Name'),
max_length=255,
help_text=_('Name by which the household goes by'),
db_index=True
class Person(models.Model):
uuid = UUIDField(auto=True, version=4, help_text=_('unique id'))
last_name = models.CharField(_("Last name"), max_length=255, help_text=_('last name'), db_index=True)
first_name = models.CharField(_("First name"), max_length=255, blank=True, null=True, help_text=_('first name'))
middle_name = models.CharField(_("Middle name"), max_length=255, blank=True, null=True, help_text=_('middle name'))
location = models.ForeignKey(Location, verbose_name=_('Location'), help_text=_('location'))
household = models.ForeignKey(
Household,
related_name='active_or_inactive_members',
blank=True,
null=True,
verbose_name=_("Household"),
help_text=_('Household')
)
我正在使用此查询来获取一个家庭中的所有重复人员:
首先过滤办公室中的所有家庭
households = Household.objects.filter(office='so-co')
然后我遍历这些家庭以过滤具有重复ID的人
for household in households:
persons = Person.objects.values('last_name').annotate(Count('id')).order_by().filter(id__count__gt=1)
for person in persons:
print person
问题是我的查询集正在打印所有人,我在这里错过了什么?
答案 0 :(得分:0)
您没有在for循环中过滤household
。您可能会使用Persons
related_name
persons = household.active_or_inactive_members.all()
要查找last_name
上的重复项:
persons = household.active_or_inactive_members.values('last_name')...... # your code here