Django adds many objects to a user object

I am creating a Network class that has a lot to do with the Django User class. In other words, a Network can have many User , and each User has only one Network . It's easy to add many-to-many relationships because I can add a many-to-many field in the Network class; however, it is difficult to apply many-to-one relationships since I need to add a foreign key to the User class. I can not do this.

I have a UserProfile class that has a one-to-one relationship with User , but it is intended only to store additional information about User , and not for any relationship. All of my other relationships are related to User , not UserProfile .

Is there a way to create a many-to-one relationship with the User and Network classes without using UserProfile ? Thanks!

+4
source share
1 answer

The only thing I can think of is subclassing User , which is difficult because the administrator has no way to turn the base object ( User in this case) into a subclass object ( NetworkUser , and yes, I "tried") or creating a NetworkUser model with the relation OneToOne to User and ForeignKey - Network . I would recommend the latter, because while you are sure that you just want to add one thing to User , and this should not be a big problem, more will be added later.

The thing that bothers me that I use User directly is that it is only for one application, not necessarily for the whole project, and changing User in a way that will affect other applications in the project.

+2
source

Source: https://habr.com/ru/post/1332644/


All Articles