Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any alternatives for OneToMany or ManyToOne in Django

I have a NOTIFICATION and an USER app in Django. The code goes something like :

class Notification(models.Model):
    user = models.ForeignKey(User , related_name = "notification"
    ....
    ....

and

class User(models.Model):
    notifications = models.OneToManyField(Notification , related_name = "user"
    ....
    ....

Now I know that models.OneToManyField doesn't exist in Django. I get the fact that I can simply access the user from the notification instance of the model. But I suppose that would somehow slow my system because in production I would keep all the instances of Notification Model. For example : I'm expecting around 500+ notifications per user once the system is in production for a significant amount of time.

I suppose, it would just be easier to access all the notifications of one user directly rather than sifting through the whole Notification Table to find notifications of a specific user.
I've read this and the documentation to an extent and I'm not able to find a solution to my problem.

Also I'm not sure about the processing constraints of a processor to obtain all the Notifications from the whole Notification Table. I'm just assuming that it'll be somewhat slower.

like image 677
Shivam Sharma Avatar asked Mar 11 '23 04:03

Shivam Sharma


1 Answers

OneToManyField doesn't exist in Django because it is just the reverse relationship of a ForeignKey. So you don't need the notifications field on the User model here, just remove it.

Don't worry prematurely about performance of filtering on the notifications. Querying relations is what SQL was designed for, and that's what relational databases are good at doing.

like image 199
wim Avatar answered Mar 13 '23 02:03

wim