Reputation: 37904
I have one understanding probem:
I want to create user profile for my page, which will have many attributes. But since I am new to User Model of django's auth, i dont know if i can extend that model with new coming attributes that i set for user profile. e.g. address, age, ...
if not, why is User Model there? is it only for auth purposes? how do I handle and differentiate these two User models?
Sample Case:
user wants to log in, I check the permission with auth's user model and set some relation between django's user model and mine? I just kind of confused here
Upvotes: 1
Views: 354
Reputation: 353
Since Django 1.5 you can create your own User Model with your custom fields. And tell Django to use that model for auth.
Link to documentation: Custom User Model
Update: If you are using Django 1.4, then django-primate can help you to define custom user model.
Upvotes: 1
Reputation: 99660
I shall try to explain in the perspective of pre-django 1.5
The django's auth models provides a set of "convenience" methods like login, logout, password reset, etc for you which work seamlessly. It is a very common scenario to have more fields - so One approach would be to create a userprofile model, which either inherits
from the django auth user model, or has a OneToOne
relationship on the same. This way, you do not have to reimplement some of the already implemented features. In addition, there is groups
and permissions
models in the package which add a whole layer of functionality in terms of user permissionsing.
example:
from django.contrib.auth.models import User
class MyCustomProfile(User):
#inherits all the attributes of default `User` models
#additional models here.
OR
from django.contrib.auth.models import User
class MyCustomProfile(models.Model):
user = models.OneToOneField(User)
#additional models here.
This way, you can use the all the features and build on top of it.
This has changed a little bit in django-1.5 which lets users have Custom fields, which saves you from creating a UserProfile
model on top of the already existing User
model.
Upvotes: 2
Reputation: 2597
Derive your class from the user model.
from django.contrib.auth.models import User
class MyCustomUser( User ):
# my properties here
# my custom methods here
Then you need a custom backend to handle passing your custom user class instead of the regular user so that you can access the properties of that custom user class.
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import check_password
from my.app.models import MyCustomUser
class CustomUserModelBackend(ModelBackend):
def authenticate(self, username=None, password=None):
try:
user = MyCustomUser.objects.get(username=username)
if user.check_password(password):
return user
except Player.DoesNotExist:
return None
return None #Password was wrong
def get_user(self, user_id):
try:
return MyCustomUser.objects.get(pk=user_id)
except Player.DoesNotExist:
return None
Then add the backend to your AUTHENTICATION_BACKENDS
tuple in your settings.py
. You can customize it further, this is a simple implementation.
Upvotes: 1