Skip to content

Instantly share code, notes, and snippets.

@DanyF-github
Created March 24, 2022 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanyF-github/92ac1931b2ea3f8411139ddb37bb06f3 to your computer and use it in GitHub Desktop.
Save DanyF-github/92ac1931b2ea3f8411139ddb37bb06f3 to your computer and use it in GitHub Desktop.
django.contrib.contenttypes.models import ContentType
from django.db.models import Value
from itertools import chain
@property
def messages(self):
from conversation.models import Message
message_type = ContentType.objects.get_for_model(self)
msgFromLead = Message.objects.filter(sender_id=self.id, sender_type=message_type).annotate(
from_lead=Value(True, models.BooleanField())
)
msgToLead = Message.objects.filter(receiver_id=self.id, receiver_type=message_type).annotate(
from_lead=Value(False, models.BooleanField())
)
messages = sorted(
chain(msgFromLead, msgToLead),
key=lambda instance: instance.date_created
)
return messages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment