Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django如何在多个配置文件中使用AUTH_PROFILE_模块?_Python_Django_Django Models - Fatal编程技术网

Python django如何在多个配置文件中使用AUTH_PROFILE_模块?

Python django如何在多个配置文件中使用AUTH_PROFILE_模块?,python,django,django-models,Python,Django,Django Models,假设我对不同的用户类型有不同的配置文件-员工、教师、学生: 如何指定AUTH_PROFILE_模块,以便使用get_PROFILE返回相应的配置文件 没有办法做到这一点,但您可以使用通用密钥 from django.contrib.contenttypes import generic from django.contrib.contenttypes.models import ContentType class UserProfileOne(models.Model): pass

假设我对不同的用户类型有不同的配置文件-员工、教师、学生:


如何指定
AUTH_PROFILE_模块
,以便使用
get_PROFILE
返回相应的配置文件

没有办法做到这一点,但您可以使用通用密钥

from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType

class UserProfileOne(models.Model):
    pass

class UserProfileTwo(models.Model):
    pass


class UserProfile(models.Model):
    content_type    = models.ForeignKey(ContentType)
    object_id       = models.PositiveIntegerField(db_index=True)
    content_object  = generic.GenericForeignKey('content_type', 'object_id')
例如:

UserProfile.objects.create(content_object=any_profile_instance)

User(pk=1).get_profile().content_object.some_special_field

如果您可以提供更多信息,那么在可能找到更好的解决方案时:)

尝试使用多个配置文件和简单的UserProfile配置可能会更麻烦

我建议使用带有通用外键的UserProfile,或者完全抛弃UserProfile,使用用户外键创建单独的模型

如果要将大量数据附加到这些额外的用户配置文件,我会将它们保存在与UserProfile分开的模型中。我个人对UserProfile的看法是,它可能很快成为没有好家的用户数据的垃圾场

不要因为你的数据听起来像是配置文件信息,就认为你必须使用UserProfile。我不认为UserProfile是用来解决所有问题的。我认为这是一种弥补用户模型的代码在django代码下这一事实的方法,我们通常不会在添加用户数据时弄乱它