问题
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