简体   繁体   中英

How can I query in Django to a table with multiple conditions?

I have a table with fields channel_id , message_id and status . Each row in this table represents a message from a channel. The status can be SENT, DELIVERED or SEEN. When a user sends a message, a row is added to this table with the status SENT. When a user reads that message, another row is added with the status SEEN. So, for one message, I have multiple rows. I want to retrieve the rows from this table which have not the SEEN status, so I am able to know which message is not read. Is there a way of doing that with only one query to the database?

Edit: I want to make something like a sub query. So, if I have the following data:

[channel_id = 1, message_id = 1, status = 'DELIVERED'],
[channel_id = 1, message_id = 1, status = 'SEEN'],
[channel_id = 1, message_id = 2, status = 'DELIVERED']

the query gives me only the second, because I know it is the message which has not been seen.

The below query will give you a list of statuses added for each message. using this you can check weather message is been read or not.

  TableName.objects.filter(channel_id=channel_id, message_id=message_id).value_list('status', flat=True);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM