Duplicated total Summ DB Queries in admin list

Do I maybe understand something incorrect, but Sentry says me that django tries to send 2 queries like SELECT COUNT(*) AS __count from admin list view. Probably it happens due to filters when I add them in list_filter and runs even in the time when this filter wasn’t chosen in an admin web interface.

And looks like that this attribute helps to resolve an error show_full_result_count = False. But honestly I would like to see amount when I use filter or search. I just want to not use SUMM when it is not needed (filter wasn’t chosen). Probably, something wrong is happens in django here, isn’t it?

Welcome @mia !

It might be helpful if you provided more details about this issue:

  • Post the model that is causing it
  • Post the ModelAdmin class involved
  • Show the actual queries being highlighted by Sentry

This is more-less related to problem code

class Message(Model):
    # Here is public most used data of message
    class Meta:
        unique_together = ('message_in_messanger_type', 'message_in_messanger_pk')

    objects = MessageQueryset.as_manager()
    cleaned_text = TextField()  # source_text without tags, quote and another technical data
    resent_source_text = TextField(blank=True, null=True)
    time_of_sending = DateTimeField(default=now, null=False)
    updated = DateTimeField(auto_now=True)
    sender = ForeignKey(Partner, on_delete=CASCADE, blank=True, null=True, related_name='messages_where_sender')
    recipients = ManyToManyField(Partner, blank=True, related_name='messages_where_recipient')
    newsletter = ForeignKey(Newsletter, on_delete=CASCADE, blank=True, null=True)
    reason_of_not_approving_to_resend = TextField(
        blank=True) 
    deleted = DateTimeField(blank=True, null=True)
    resent = DateTimeField(blank=True, null=True) 
    file = FileField(upload_to='messages_attachments', blank=True, null=True)
    image = ImageField(upload_to='messages_images', blank=True, null=True)
    applied_white_text = ManyToManyField(WhiteText, blank=True)

    source_message_of_resending = OneToOneField('self', blank=True, null=True, on_delete=CASCADE,
                                                related_name='current_sending_message') 
    message_in_messanger_type = ForeignKey(ContentType, on_delete=CASCADE, blank=True,
                                           default=SkypeMessage.contenttype_pk, related_name='message')
    message_in_messanger_pk = CharField(max_length=300, blank=True)
    message_in_messanger = GenericForeignKey('message_in_messanger_type', 'message_in_messanger_pk')

    deserialize_cleaned_text_to_model = ForeignKey(ContentType, on_delete=CASCADE, blank=True, null=True,
                                                   related_name='source_message') 
    deserialize_cleaned_text_to_id = PositiveIntegerField(blank=True, null=True)
    deserialize_cleaned_text_to = GenericForeignKey('deserialize_cleaned_text_to_model',
                                                    'deserialize_cleaned_text_to_id')
    prediction = JSONField(blank=True, null=True)

class AccessedParticipant(admin.SimpleListFilter):
    title = 'Sender/recipient partner'
    parameter_name = 'accessed_participant'

    def lookups(self, request, model_admin):
        # This is the list how will display partners names for filter options in right admin panel
        messages = model_admin.get_queryset(request)
        recipients_and_senders_ids = messages.get_participants_ids()
        accessed_partners = Partner.objects.filter(pk__in=recipients_and_senders_ids)
        return ((partner.pk, partner.accessed_name(request.user)) for partner in accessed_partners)

    def queryset(self, request, queryset):
        # This method called when filter options in right admin defined in lookups was selected in right admin panel
        if partner_pk := self.value():
            return queryset.for_partner(Partner.objects.get(pk=partner_pk))


class MessageAdmin(ModelAdmin):
    readonly_fields = ['cleaned_text_custom', 'newsletter', 'separate_emotions']
    list_editable = ['deserialize_cleaned_text_to_model']
    list_display = readonly_fields + ['time_of_sending', 'updated', 'accessed_sender', 'accessed_recipients', 'deleted',
                                      'resent',
                                      'reason_of_not_approving_to_resend', 'white_text_applied'] + list_editable
    list_filter = [AccessedParticipant] # TODO: Consecutive DB Queries of COUNT(*) AS "__count" happens.
    # It will be more useful when partners will be saved to db and search by them will be introduced in this filter.
    list_display_links = ['cleaned_text_custom', 'time_of_sending', 'updated']
    list_select_related = ['sender', 'message_in_messanger_type']
    search_fields = ['cleaned_text', 'accessed_sender', 'accessed_recipients']
    form = MessageAdminForm
    list_per_page = 30

Attach you info related to queries

Also, I’ve found that there is very similar topic, that is not completed yet too Django admin list does Count query twice

The issue is known and the count is done twice when there are no filters.

However the fix for this was deemed to risky since the admin filters are not the only ones that can change the count.
For example when involving custom managers or querysets, since those can come prefiltered, which means you have to do the count twice.

You can see the ticket here #34593 (Django admin list does same Count query twice when there are no filters) – Django

1 Like

yes, I agree with you. I got errors without filters too. But generally show_full_result_count = False resolved the problem.