Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could either do it in python (as suggested above) or let the DB do the dirty work for you.</p> <p>You would need to construct a QuerySet containing all threads with a deleted message for a particular user and then a second QuerySet that gets all threads not contained in the first:</p> <pre><code>delthreads = MessageThread.objects.filter(messagerecipient__recipient=user, messagerecipient__status='deleted').distinct() result = MessageThread.objects.filter(messagerecipient_recipient=user) .exclude(messagethread_in=delthreads).distinct() </code></pre> <p>However, performance wise (especially on large tables) this will hurt you. Your model needs some restructuring to allow for more efficient queries. Consider:</p> <pre><code>class MessageThread(models.Model): subject = models.CharField(max_length=256, blank=False) class Message(models.Model): thread = models.ForeignKey(MessageThread) content = models.CharField(max_length=5000, blank=False) timestamp = models.DateTimeField(auto_now_add=True, blank=False) sender = models.ForeignKey(User) recipient = models.ForeignKey(User) status = models.CharField(max_length=20, choices=MESSAGE_STATUS, default="unread") </code></pre> <p>If you anticipate you will execute this query often, you could add a cache field to MessageThread which you can update whenever someone deletes a message in that thread:</p> <pre><code>class MessageThread(models.Model): subject = models.CharField(max_length=256, blank=False) dirty = models.BooleanField(default=False) </code></pre> <p>Then, it would be very easy to extract a list of threads with no deleted messages.</p> <pre><code>nondelthreads = MessageThread.objects.filter(message__recipient=user, dirty=True).distinct() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload