2013-11-01 40 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對象。