我想使用Django为两个外键添加两个注释。我该怎么做?

我想使用Django为两个外键添加两个注释。我该怎么做?,django,foreign-keys,Django,Foreign Keys,在上述情况下,计数不正确。如何获得正确的计数?您想对不同的外键模型实例进行计数 将注释功能更新到以下位置: # Run output = add_families_and_locations_counts(elephants) 这回答了你的问题吗? def add_families_and_locations_counts(elephants): return elephants.annotate( families_number=Count('families

在上述情况下,计数不正确。如何获得正确的计数?

您想对不同的外键模型实例进行计数

将注释功能更新到以下位置:

# Run
output = add_families_and_locations_counts(elephants)

这回答了你的问题吗?
def add_families_and_locations_counts(elephants):
    return elephants.annotate(
            families_number=Count('families'),
            location_number=Count('location')
    )
# Run
output = add_families_and_locations_counts(elephants)
def add_families_and_locations_counts(elephants):
    return elephants.annotate(
            families_number=Count('families', distinct=True),
            location_number=Count('location', distinct=True)
    )