Reputation: 2413
I have been having a hard time accomplishing this task and it seems that I cannot get help anywhere I turn. I am trying to send Memo
s to specific user group
s in Django. Each user in each group
should receive the Memo
and be able to change the BooleanField
to True
to signify that they have read it.
I then need to be able to access the amount of users which received and have marked the BoolenField
as True
so I can create a table in a template which says [3/31 Read] and such.
I would like to not use GenericForeignkey
s if possible and I would like to keep it as one Model if possible but I know that may not work. Currently I was trying this:
class Memo(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_time = models.DateTimeField(default=timezone.now)
sender = models.ForeignKey(User, on_delete=models.CASCADE)
receiver = models.ManyToManyField(Group)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('memos-detail', kwargs={'pk': self.pk})
I was going to access each user (receiver) within the group which is selected on the MemoCreateForm
then apply that user to this model:
class ReceivedMemo(models.Model):
memo = models.ForeignKey(
Memo, related_name='user_memos', on_delete=models.CASCADE)
receiver = models.ForeignKey(
User, related_name='memos', on_delete=models.CASCADE)
read = models.BooleanField(default=False)
Then I was going to try to filter the ReceivedMemos
by memo
to see if each receiver
has read
the memo or not. But this is starting to get complicated and I am not sure if it will work. Am I going about this the right way? Or should I be able to have one Model such as:
class Memo(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_time = models.DateTimeField(default=timezone.now)
sender = models.ForeignKey(User, on_delete=models.CASCADE)
receiver = models.ForeignKey(User, on_delete=models.CASCADE)
read = models.BooleanField(default=True)
Seems that each object would have the BooleanField
applied to the object and not the user though.
Upvotes: 0
Views: 103
Reputation: 16050
The ReceivedMemo
model seems more appropriate rather than the read
bit, but the issue with this approach is that whenever you create a new Memo
you need to also create lots of (for every User
in the Group
) ReceivedMemo
objects with read=False
? This seems pointless. Maybe you can just store the Users which actually read this thing, and for everyone that's left, consider he has not read it. I.e.
class Memo(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_time = models.DateTimeField(default=timezone.now)
sender = models.ForeignKey(User, on_delete=models.CASCADE)
receiver = models.ManyToManyField(Group)
read_by = models.ManyToManyField(User)
Upvotes: 1