开发者

Django add a many to one field to user object

I'm creating a Network class that has a many-to-one relationship 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 the many-to-many field in the Network class; however, it's hard to apply many-to-one relationship since I need to add the the foreign key in User class. There is no way I can do that.

I do have a UserProfile class that has a one-to-one relationship with User, but it's only for storing additional information about the User, not any relationships. All my other relationships defined are relating to User, not UserProfile.

Is there a way to create a many-to-one rela开发者_运维知识库tionship to the User and Network class without using the UserProfile? Thanks!


The only things I can think of are subclassing User, which is tricky because the admin has no way to turn a base object (User in this case) into a subclassed object (NetworkUser, and yes I've tried), or creating a NetworkUser model with a OneToOne relationship to User and a ForeignKey to Network. I'd recommend the latter, because while you feel sure now that you just want to add one thing to User and it shouldn't be a big deal, later on there will be more to add.

The thing that bothers me about directly messing with User is that this is only for a single app, not necessarily the whole project, and modifying User in a way that will affect other apps in the project.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜