[Answered ]-Extend Django User model (make it work in request.user), Django 1.2.3

2👍

You’re on the right track with adjusting the Authentication Backend.

Authentication backends can be pretty simple, here is a snip of ours we use in our project.

class LocalAccount(object):
    def authenticate(self, system=None, username=None, password=None):
        try:
            user = User.objects.get(system=system, alias=username)
            if user.check_password(password):
                return user
        except User.DoesNotExist:
            return None

    def get_user(self, user_id):
        'returns user'

    def get_all_permissions(self, user_obj):
        'returns user permissions'

    def has_perm(self, user_obj, perm):
        'verifies permission, remember you can hardcode these'

    def get_group_permissions(self, user_obj):
        return set() #We don't use this

    def has_module_perms(self, user_obj, app_label):
        return False

This allows you to return whatever user model you wish to return. As long as your user model is basically like Django’s user model you won’t run into any issues.

Also keep in mind that extending user and including the application will cause both the Django User model and your own User model to be in the database with model inheritance. Your best option is to copy the Django User model and alter it. For our project we don’t even include ‘auth’ in the installed apps setting.

The concept here is Python duck typing. As long as the User model that you create has the same interface that Django is looking for (methods, fields) it doesn’t matter if it’s actually the User class it specified.

👤Xealot

Leave a comment