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