nelsonvarela
nelsonvarela

Reputation: 2370

Django authentication with custom model User

For my needs builtin model User is not enough... So I have my own model UserProfile and I want make authentication on site through this model (UserProfile does not inherit from User model and not related to it at all).

My Model:

class UserProfile(models.Model):
    password = models.CharField(max_length = 40)
    email = models.EmailField(max_length = 72, unique = True)

    ## Add this so that you can use request.user.is_authenticated
    def is_authenticated(self): 
        return True

But builtin authentication uses model User.

So I want to understand how can I change that, so authentication use my model UserProfile with all auth features???

A good tutorial would be great!

(Step by step in views, models and authentication)

PS: I know I can store extra data in other model but I don't want that

Upvotes: 2

Views: 1559

Answers (1)

Mark Lavin
Mark Lavin

Reputation: 25164

Here's an even more extreme example but illustrates that what you want to do can be done. The author not only replaces the User model which the authentication backend uses but also uses SQLAlchemy instead of the Django ORM. http://tomforb.es/using-a-custom-sqlalchemy-users-model-with-django

The main point is that you need to write your backend authenticate and get_user methods to retrieve your custom User model. If you want to also support permissions you would need to write has_perm.

Upvotes: 3

Related Questions