I am trying to implement a notification page on my website in Django. But I have a problem that how can I send the same notification to multiple users.
let suppose I have to send it to just one user.
then I can create a model of notification with
noti = models.TextField()
user= models.foreignkey
is_seen = models.bool
so this is just a sample
but the the problem is this how i can send this notification to selected multiple users
one important is that is_seen is compulsory for each user
I hope you will understand
I think it depends how this data will grow in future. Because if all of your notifications go to multiple users, you can have a Notification model with ManyToMany relation with User. Something like following
class Notification(models.Model):
message = models.TextField()
users = models.ManyToMany(User, through='UserNotification')
class UserNotification(model.Model):
user = models.ForeignKey(User)
notification = models.ForeignKey(Notification)
is_seen = models.BooleanField()
But other question is if you want to build something like the one you shared, that is also fine. I don't think so there's an issue of adding same message for multiple users. This is more flexible in my opinion as compared to M2M relation. You can extend this in future easily as your application will grow.
class Notification(model.Model):
message = models.TextField()
user = models.ForeignKey(User)
is_seen = models.BooleanField)
Another case can be if you want to have more relations and if there's need of adding more information related to notification. This gives more room for improvement in future in terms of storing information.
class Notification(model.Model):
message = models.TextField()
class UserNotification(model.Model):
user = models.ForeignKey(User)
notification = models.ForeignKey(Notification)
is_seen = models.BooleanField()
Related
I am new to django. I have a model like this:
class Standup(models.Model):
team = models.ForeignKey("Team", on_delete=models.CASCADE)
standup_time = models.DateTimeField(auto_now_add=True)
class StandupUpdate(models.Model):
standup = models.ForeignKey("Standup", on_delete=models.CASCADE)
employee = models.ForeignKey("Employee", on_delete=models.CASCADE)
update_time = models.DateTimeField(auto_now_add=True)
status = models.CharField(max_length=50)
work_done_yesterday = models.TextField()
work_to_do = models.TextField()
blockers = models.TextField()
If I write view for this model, every employee will have to hit API for his/her standup update. But I am supposed create a single API which takes updates of all the employees and saves it into database. In frontend, it will be something like this:
Employee will select on a team as one employee can be a part of
multiple teams.
Then the employee will give his/her stadup updates.
Then another employee will do the same thing and so on.
At the end,by clicking on submit button, whole data will be saved together.
Any guidance on how to do it?
Not sure why you need a separate model for Updates.
I would try to approach it like that:
make the Standup model reference both Team and Employee models;
last_update, status, work_to_do etc. as its fields;
make a custom serializer that accepts a list with Standup field values and takes the authorized user's ID from request object's data. last_update time can be now(), status calculated according to your business logic
This part of DRF documentation could probably be helpful.
I have a built-in User table and a Note table associated with it by key.
class Note(models.Model):
header = models.CharField(max_length=100)
note_text = models.TextField()
data = models.DateField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
That is, a registered user may have several notes. How do I get all these notes from this particular user (who has now visited his page)? I need to get these notes in views.py.
I tried different ways, don't know how to do that.
Hope this answer finds you well ...
First, you already have a user object in your request. But, still you want to filter out notes for other users too, then do
get_user = User.objects.get(id=id) # For any random user
get_user = request.user # For authenticated user
After getting the user, you simply filter it with the Note model like this ...
get_notes = Note.objects.filter(user=get_user)
You are good to go!
I'm creating a project using Django and I'm wondering how I can make objects distinct. Now I am creating message box part and what I want to do is show up user names who the user have been contacting. My current way is like this
models.py
class Message(models.Model):
''''''
text = models.TextField()
created = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='myself')
someone = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='someone')
views.py
def inbox(request):
'''See messages history'''
messages = Message.objects.filter(user=request.user).distinct('someone')
notification = Message.objects.filter(someone=request.user).distinct('user')
return render(request, 'campus_feed/inbox.html', {'messages': messages})
So what I want to do is list the users who is contacting me or I contacted and I don't want to show up the same name multiple times but I cannot come up with good way to filter them. Anyone who can give me tips?
If you want users, you should request users.
User.objects.filter(Q(myself__someone=request.user) | Q(someone__user=request.user))
This gives you all users who are either the "someone" in a message from the current user, or the "user" in a message to the current user. Since you are querying users directly, the is no possibility of duplicates.
As I am beginning to work my way through web-sockets using Python (Django Channels in this case), I am beginning to wonder how companies scale chat applications.
Take this specific problem:
User A has a list of "group messages" that they are involved in. User A is capable of receiving notifications from each of the groups. Lets suppose User A has 5 different groups he would like real time notifications for.
Does User A have 5 separate web socket connections? This is easiest, but the bulkiest and surely wont scale (what if we had 20 chats per user). It is quite easy to make a Django Channels "Group" that listens and sends notifications to one specific group of people.
Does User A instead have 1 web socket connection taking all notifications from many different locations? If so, what is User A subscribing to on the backend? In Django Channels, a websocket is made when users subscribe to a "Group". If we wanted one user per "Group', this would seem counter intuitive. In this case, User A is subscribing to their own personal "Group" and other backend services are delivering messages to that specific "Group" based on other logic that is implemented when a message is received from any user using the service.
This is very verbose and text heavy, but it diagrams an interesting problem that has scaled for other companies in the past. I appreciate any information or insight!
(this is a list of the problems that I thought were basically the same (ignoring language and library use))
1. Websocket multiple Channels vs. One Channel + Server Side handling [GroupChat]
first create a profile model:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, verbose_name=_("user"))
channel = models.CharField(unique=True, max_length=255, blank=True, null=True, verbose_name=_("channel")) # channel for notify user
then create a message model:
class Message(models.Model):
to_user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=_("user"))
from_user = models.ForeignKey(User)
text = models.TextField(blank=True, null=True)
then use Django signals for sending notify:
#receiver(post_save, sender=Message)
def send_notify(instance, created, **kwargs):
if created:
Group('chat-{}'.format(instance.to_user.profile.channel)).send({'text': {'message': 'you have a new message'}})
sorry for my english : ))
for example i have a model with booleanfield
class Item(BaseModel):
name = models.CharField(max_length=255)
pending = models.BooleanField(default=False)
if user creates new item from admin panel pending field is false and this item won't display on website until other user set this field into True, but this user mustn't be allowed to make change on pending field on his items but he can do this activity on other users items. any solutions?
There are a number of ways to implement this, depending on how you are ultimately going to end up using it. The most straightforward is to add a foreignkey on your Item to the user (assuming you use django auth).
from django.contrib.auth.models import User
class Item(BaseModels):
created_by = models.ForeignKey(User)
name = models.CharField(max_length=255)
pending = models.BooleanField(default=False)
assuming this only ever gets exposed via views, then you can just do:
def some_view(request):
if item_being_edited.created_by = request.user and not item_being_edited.pending:
#rejection goes here
#do stuff
as a sidenote, change your pending field. Either have pending = True mean an item is pending, or have it to be approved = False. It will make things easier later if it makes sense to read it in your head without gymnastics.