开发者

django how to use AUTH_PROFILE_MODULE with multiple profiles?

Assuming I have different prof开发者_运维知识库iles for different user types - staff, teacher,students:

How do I specify AUTH_PROFILE_MODULE in order to get back the appropriate profile with get_profile?


There no way to do that but you could use the generic key.

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')

Example:

UserProfile.objects.create(content_object=any_profile_instance)

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

If you could provide more infos, when it might be possible to find a better solution :)


It may be more hassle to try to use multiple profiles with the simplistic configuration of UserProfile.

I would suggest using UserProfile with generic foreign key, or completely ditch UserProfile and create separate models with a User foreign key.

If you are going to have a lot of data attached to these extra user profiles, I would keep them in models separate from UserProfile. My personal opinion about UserProfile is that it can quickly become the dumping ground for user data that doesn't have a nice home.

Don't think that you have to use UserProfile just because your data sounds like it is profile information. I don't think UserProfile was ever intended to solve all the problems. I see it as a way to make up for the fact that the code for the User model is under django code and we don't generally mess with it to add user data.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜