sergiuz
sergiuz

Reputation: 5539

"Partial Inheriting" from a Django model

I know partial inheriting it is not possible. I need to create a user model for my Django apps, for that I want to use the User class defined in Django. But I do not need the username field, which is set as required.

Suppose I inherit from User to MyUser. Is it possible to delete the username attribute in MyUser. i.e. del MyUser.username? Or can someone suggest other way to do this?

Upvotes: 1

Views: 99

Answers (2)

Burhan Khalid
Burhan Khalid

Reputation: 174708

The username field is required - otherwise users cannot login.

If you want to have an email address as the username - starting from django 1.5, you can create your own custom user model which will work just like the default model that comes with django.

In this custom user model, set the USERNAME_FIELD property to the name of the column that you want to use for the username. This column must be unique.

There is a lot of work you have to do, and really this should be done at the start of your project as these changes require database updates.

You should read the documentation to get started.

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599956

The AbstractBaseUser class is made specifically for this scenario. Inherit from this class and define your own fields, and be sure to set the USERNAME_FIELD attribute to your email field.

Upvotes: 2

Related Questions