开发者

Django-registration profile fields

I am trying to add a location field to a user's profile in django-registration.

I have added the following model:

# in userprofile/mode开发者_如何学运维ls.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'

This is working -- the get_profile() works when I manually add data into the db. However, I am having trouble figuring out how to add the profile information during registration via signals. This is what I currently have:

# 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

How would I add add location=USA using signals in the above __init__.py function.


in settings.py add

LOCATION = 'USA'

in __ 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

but not's a nice way for doing things, adding code of app

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜