Is it possible to have 2 profile models using django's AUTH_PROFILE_MODULE

你说的曾经没有我的故事 提交于 2019-12-10 12:02:20

问题


Is is possible to have 2 models to store extra user details. I ask because I'm currently writing an app which i want to plug into a few existing projects. However all these projects already use a profile model, and my new app needs to extend the user model through the profile model. If it isn't is there a way of extending the currently in use profile model set within AUTH_PROFILE_MODULE?


回答1:


Shouldn't you just create a field in this other model that has a foreign key to the profile?

So assuming you have an already existent UserProfile model.

Class UserProfile(models.Model):
  user = models.OneToOneField(User)
  ...

You could extend the UserProfile model by adding a foreign key in your other model, linking to it.

Class OtherModel(models.Model):
  profile = models.ForeignKey(UserProfile, related_name="other_model")
  field1 = models.CharField(max_length=15)
  ...

And so you can access this other model from the User object:

user.get_profile().other_model.field1

HTH



来源:https://stackoverflow.com/questions/4634256/is-it-possible-to-have-2-profile-models-using-djangos-auth-profile-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!