Since you are talking about the Django User
model, you should create your own model, extending the User
. To do this, you just need to create a model with a OneToOne
relation with the User
model:, you should create a new User model, by extending AbstractUser
:
from django.contrib.auth.models import AbstractUser
class MyUserModel(AbstractUser):
# AbstractUser has already every fields needed: username, password, is_active...
def myfunc(self):
# Just a dummy working example
return "My Username in uppercase: %s" % self.username.upper()
And put AUTH_USER_MODEL = "yourapp.MyUserModel"
in settings.py
Then, you can use this new user model as if it was the User
: it has the same methods than User
(like create_user
, check_password
...), and you can access to myfunc
with user.myfunc()
, where user
is the regular MyUserModel
instance (get from request.user
or the ORM). This is more consistent with the framework organisation and allow you to add more fields to the User
if you want.
Side-note: As @Daniel Roseman pointed out in comments, you should really extend the AbstractUser
model now (since Django 1.6) instead of make an UserProfile "proxy".
Related questions: