This is what message_user_id
contains:
message_user_id = Message.objects.all()
This is my context dictionary in admin.py
:
'msg' : message_user_id,
This is my Message
class in models.py
:
class Message(models.Model):
message_text = fields.EncryptedTextField(blank=True, null=True)
conversation_id = models.ForeignKey(Conversation, on_delete=models.CASCADE)
origin_user = models.ForeignKey(UserInfo, on_delete=models.CASCADE, blank=False, default=None)
timestamp = models.DateTimeField(default=datetime.now, blank=True)
This is UserInfo
class:
class UserInfo(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
is_buyer = models.BooleanField(default=False)
is_seller = models.BooleanField(default=False)
is_professional = models.BooleanField(default=False)
There are different roles for users that they can choose from while sign up. That information stays in UserInfo
table which I'm referencing to as a foreign key in origin_user
field in Message
class. origin_user
in Message
contains the same information as id
in UserInfo
.
I want to display the number of messages sent by each category of users. How do I achieve this?.
CodePudding user response:
As there are not many types you can do a search for each type and count how many messages there are, e.g. for Buyer:
messages_of_buyers = Message.objects.select_related(
'user_info'
).filter(
user_info__is_buyer=True
).count()