如何将注册用户从django-registration链接到我的应用程序模型?

时间:2013-11-01 12:45:40

标签: python django django-models django-registration

我将django-registration应用程序插入到我的项目中,效果很好。新用户可以注册并登录。但是,在我的应用程序中,我想向用户提供整个配置文件,并使用我通过django-registration获得的信息。

如何连接django-registration中的信息和我自己应用的用户模型?

from django.contrib.auth.models import models as  auth_models
from django.contrib.auth.models import User

class User(User):

    username = auth_models.CharField(max_length=100)
    description = auth_models.TextField()
    picture = auth_models.ImageField(upload_to=something, blank=True, null=True) 
    location = auth_models.CharField(max_length=200) # please replace with GIS API

    email = auth_models.EmailField()
    password = auth_models.CharField(max_length=128)

    # during registration: did the user click
    # the sent activiation link?
    is_active = auth_models.BooleanField(default=False)
    appear_in_public_ranking = auth_models.BooleanField(default=True)

    def __unicode__(self):
        return self.username

1 个答案:

答案 0 :(得分:2)

在Django 1.5中,引入了extending the existing User model。您可以直接使用它来添加新字段。

但是如果您使用的是以前版本的Django,请执行以下操作:

创建一个名为Profile的新模型,并将所有必填字段放在那里。

from django.contrib.auth.models import User

class Profile(models.Model):
    user = models.OneToOneField(User)
    ...

然后在您的设置文件中添加:

AUTH_PROFILE_MODULE = '<application_name>.Profile'

其中application_name是您在其中创建Profile模型的应用程序的名称。

当您有登录用户时,可以使用request.user.get_profile()来获取相应的个人资料对象。