For the following unread definition, how would I get the current user to be able to count the number of MessageThreads a given user has?
class MessageThread(models.Model):
subject = models.CharField(max_length=256, blank=False)
def unread(self):
return self.objects.filter(***messagerecipient__recipient='current user'***).distinct().count()
class MessageRecipient(models.Model):
message = models.ForeignKey(Message)
recipient = models.ForeignKey(User)
status = models.CharField(max_length=20, choices=MESSAGE_STATUS, default="unread")
You have to pass the request to the method:
but this doesn’t make sense as you are calling a model method (which operates on a particular rows, i.e. a single MessageThread) – you should write a model manager to operate on the entire table (i.e. find all unread threads in the table MessageThread)
so now you can call: