I've extended Django's default user class like this:
class CustomUser(User):
friends = models.ManyToManyField('self', symmetrical=False)
But now I want to use that everywhere instead of the default User
class. In my view methods, I have access to request.user
but that's an instance of the User
class. What's the easiest way to make it return a CustomUser
instead?
you can also "monkey patch" the existing User model somewhere in your models.py and it should just work. In your code just keep using the original class.
User.add_to_class('friends',models.ManyToManyField('self', symmetrical=False))
I'm not sure if you can do that exactly, but you can access your CustomUser attributes using the Django user_profile feature.
In your settings.py:
AUTH_PROFILE_MODULE = 'myapp.CustomUser'
In your view:
user_friends = user.get_profile().friends
Check out this link for more info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With