abisko
abisko

Reputation: 743

Override Django User Model

I'm writing a Django app. I wanted to override the Django User Model for the following reasons:

1) I want to use LDAP for authentication. Therefore, Django authentication is not necessary for me.

2) I already have a user table and I want to reuse that table. I do not want Django to create a dup table for me. Also the table structure doesn't "fit" Django User model very well.

Therefore, I'd like to override the Django User class.

Does anyone have any good example good that I can learn from?

Upvotes: 0

Views: 1340

Answers (2)

FlipperPA
FlipperPA

Reputation: 14311

If you want to use your own user table, you'll want to look into AbstractBaseUser, which is documented here:

https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#django.contrib.auth.models.AbstractBaseUser

Using it, you have to define which field will serve as your username in the model, using USERNAME_FIELD. You may also want to set the model to be managed = False if you want to manually apply changes and keep the model and database table in sync yourself.

I have had the best luck with Django-python3-ldap: https://github.com/etianen/django-python3-ldap

We've been using it in production with Python 3 for several years now with OpenLDAP, and it also supports Active Directory. Good luck!

Upvotes: 0

Chris Curvey
Chris Curvey

Reputation: 10389

there are already django libraries for authenticating with LDAP. I think I have used django-auth-ldap in the past successfully. (Although I usually have to get an LDAP expert to help me with the configuration.)

The documentation has instructions on how to implement a custom user model. It is far easier to do this first (before you have even run your first migration).

Upvotes: 2

Related Questions