I plugged the django-registration app into my project and it works great. New Users can register and log in. In my app, however, I want to supply a whole profile to the user and use the information which I got through django-registration.

How can I connect the information from django-registration and the user model of my own app?

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
有帮助吗?

解决方案

In Django 1.5, extending the existing User model has been introduced. You can use this directly to add new fields.

But if you are using a previous version of Django, then do the following:

Create a new model called Profile and put all your required fields there.

from django.contrib.auth.models import User

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

Then in your settings file, add:

AUTH_PROFILE_MODULE = '<application_name>.Profile'

where application_name is the name of app in which you've created Profile model.

When you have a signed in user, you can use request.user.get_profile() to fetch the corresponding Profile object.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top