2013-11-01 44 views
0

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

如何连接django注册信息和我自己的应用程序的用户模型?

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 

回答

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' 

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

如果您拥有登录用户,则可以使用request.user.get_profile()来获取相应的Profile对象。