Django注册配置文件字段

时间:2011-05-24 23:33:15

标签: django django-models django-signals django-registration

我正在尝试在django-registration中为用户的个人资料添加位置字段。

我添加了以下模型:

# in userprofile/models.py
class Profile(models.Model):
    user = models.ForeignKey(User, unique=True)
    location = models.CharField(max_length=100, blank=True)

# in settings.py
AUTH_PROFILE_MODULE = 'userprofile.Profile'

这是有效的 - 当我手动将数据添加到数据库时,get_profile()可以正常工作。但是,我无法确定如何在注册过程中通过信号添加配置文件信息。这就是我目前所拥有的:

# in backend/default/__init__.py
def register(self, request, **kwargs):
    username, email, password = kwargs['email'], kwargs['email'], kwargs['password1']
    if Site._meta.installed:
        site = Site.objects.get_current()
    else:
        site = RequestSite(request)
    new_user = RegistrationProfile.objects.create_inactive_user(username, email, password, site)
    signals.user_registered.send(sender=self.__class__,
                                 user=new_user,
                                 request=request)

    # how to add 'location = USA' to profile via signals here?

    return new_user

如何使用上述location=USA函数中的信号添加__init__.py

1 个答案:

答案 0 :(得分:2)

在settings.py中

添加

LOCATION = 'USA'
<_>在__ init __。py

# in backend/default/__init__.py
def register(self, request, **kwargs):

    username, email, password = kwargs['email'], kwargs['email'], kwargs['password1']
    if Site._meta.installed:
        site = Site.objects.get_current()
    else:
        site = RequestSite(request)
    import settings
    if settings.LOCATION:
        location = settings.LOCATION
    new_user = RegistrationProfile.objects.create_inactive_user(username, location, email, password, site)
    signals.user_registered.send(sender=self.__class__,
                                 user=new_user,
                                 request=request)


    return new_user

但不是一个很好的做事方式,添加应用代码