本文介绍了是否有可能使用Django的AUTH_PROFILE_MODULE有2个型号的个人资料的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时可以有2种型号存储额外的用户详细信息。我问,因为我目前正在写,我要插入一些现有项目的应用程序。但是,所有这些项目已经使用了剖面模型,和我的新的应用程序需要通过剖面模型来扩展用户模型。如果不是有延长目前使用的剖面模型内AUTH_PROFILE_MODULE设置的一种方式?

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?

推荐答案

你不应该只是有一个外键的轮廓这个其他型号的创建领域?

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

心连心

这篇关于是否有可能使用Django的AUTH_PROFILE_MODULE有2个型号的个人资料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 17:26