Last Updated: February 25, 2016
·
612
· adrien-f

Add a method to Django's User class

Extending the Django User is a bit of a pain for the moment. Of course you have the profile option, but I just needed to have a method deactivating an user by setting its "is_active" field to False and recording some stuff with that.

Sure it could have gone in a view, but the challenge of overriding the User class was too good to pass so here we go:

from django.contrib.auth.models import User

class UserFunctions:
    def deactivate(self):
        self.is_active = False
    ...
User.__bases__ += (UserFunctions,)

And boom ! Now you can simply use the method like a boss.

Source

1 Response
Add your response

Monkey-patching. No way.

over 1 year ago ·