Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to add convenience methods to a Django Auth User model?

I want to add a convenience/model method to the django.contrib.auth.models.User model. What is the best practice for doing this since, last time I checked, extending the User model was considered bad practice.

I have a separate custom UserProfile model. Should I be using that for all User-related convenience methods?

like image 265
Soviut Avatar asked Nov 30 '09 06:11

Soviut


3 Answers

It depends what you are trying to add to the model. If you want to add more information about the user, then it is generally recommended that you use the UserProfile method: http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users

However, if you just want to add custom methods or managers to the User model, I would say that it's more logical to use a proxy model, like so:

from django.contrib.auth.models import User

class UserMethods(User):
  def custom_method(self):
    pass
  class Meta:
    proxy=True

A proxy model will operate on the same database table as the original model, so is ideal for creating custom methods without physically extending the model. Just replace any references to User in your views to UserMethods. (And of course you can use this in the admin tool by unregistering the User model and registering your proxy model in its stead.)

Any instances of the original User model that are created will be instantly accessible via the UserMethods model, and vice-versa. More here: http://docs.djangoproject.com/en/dev/topics/db/models/#proxy-models

(NB. Proxy models require Django 1.1 and above)

like image 179
Gary Chambers Avatar answered Oct 20 '22 18:10

Gary Chambers


if you want to add custom methods to the User model, I would recommend monkey_patching:

create a file monkey_patching.py in any of your apps::

#app/monkey_patching.py
from django.contrib.auth.models import User 

def get_user_name(self):
    if self.first_name or self.last_name:
        return self.first_name + " " + self.last_name
    return self.username

User.add_to_class("get_user_name",get_user_name)

and import it in app's __init__.py file. ie::

#app/__init__.py
import monkey_patching
like image 22
suhailvs Avatar answered Oct 20 '22 18:10

suhailvs


Yes. No need to mess with the foundations when your user model has a .get_profile() function attached to it.

like image 21
SapphireSun Avatar answered Oct 20 '22 17:10

SapphireSun