django - Extending `auth.models.User` and usering login, logout - django

If I create a CustomUser model which inherits from django.contrib.auth.models.User, like so:
in models.py
class CustomUser(django.contrib.auth.models.User):
customfield = TextField()
...
Should I still be able to use
django.contrib.auth.{authenticate, login, logout} in the normal way? Do I have to make some additional configuration change? I know these methods only work on User objects, but technically my CustomUser is-a User.
Currently, authenticate(username=u, password=p) is always returning None, even with valid credentials.

Since Django 1.5 (officially but it doesn't worked for me) and "stable" in 1.6 there is a functionality to extend the User model in a clean way.
At first:
-> Take care that you load the User model only via:
from django.contrib.auth import get_user_model
User = get_user_model()
-> Once you have built the database theres no easy way to change the User model. The database relations will break and Django / South isn't able to fix it.
-> third party modules have to be compatible with that new layout and refer in it's models to "get_user_model()", too.
You have to add some Code for the admin to respect your new model:
See: https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-the-existing-user-model
To Override the model you need to inherit from AbstractBaseUser:
from django.contrib.auth.models import AbstractBaseUser
class MyUser(AbstractBaseUser):
...
date_of_birth = models.DateField()
height = models.FloatField()
...
REQUIRED_FIELDS = ['date_of_birth', 'height']
AbstractBaseUser provides you all attributes of the default user model. So you don't have to take care of email, username, first_name, last_name, password etc.
More info about overriding the user model: https://docs.djangoproject.com/en/dev/topics/auth/customizing/#django.contrib.auth.models.CustomUser
In your settings link your new model:
AUTH_USER_MODEL = 'customauth.MyUser'
Please read the whole documentation of customizing the user model, there are some interesting hints for overriding the default manager, admin forms etc. Just remember that bigger changes in an existing project can be a big pain.

A short overview:
- Extend models.AbstractUser
- Set AUTH_USER_MODEL in settings.py
All details can be found here: https://docs.djangoproject.com/en/dev/topics/auth/customizing/#specifying-a-custom-user-model

Related

Should I use the default users model in django 1.11 for my project

Actually, I'm working on a project where I need to save some details like name, username, password, age, gender etc of every user.
In that website, any user can login to their account, edit information.
So should I use the default users model or create a new model
I suggest you subclass AbstractUser. This option is suitable if you're fine with Django's User fields, but need extra fields. Django documentation also recommends to do this anyway.
If you’re starting a new project, it’s highly recommended to set up a
custom user model, even if the default User model is sufficient for
you. This model behaves identically to the default user model, but
you’ll be able to customize it in the future if the need arises:
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
pass or additional fields here ...
You also have to point to this model before creating or running any migration in the settings:
AUTH_USER_MODEL = 'yourapp.User'
Default user model in Django save some limited fields about one user. fields are
first_name, last_name, email, password, groups, user_permissions, is_staff, is_active, is_superuser, last_login, date_joined
If you want to save other information of user like birthday, expertise, gender you have to write userprofile model which must be linked one to one with user.
Example:
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OnetoOneField(User)
birthday = models.DateTimeField(default=datetime.datetime.now)
skills = models.CharField(max_length=128)
Actually, If you want to create a custom user model for user, It's mentioned in the Django's Official documentation. For achieving that you have to first inherit your custom user model with AbstractUser class and then pointing that custom user in your settings file by mentioning
AUTH_USER_MODEL = 'mycustom_user_app.MyCustomUser'.
Now django internally knows which model is the User model for the project, and you can access all model managers(e.g. create_user, etc. ) for your custom user. In that way you can use the current models fields and also can add more fields into it. That's the legit way to go with and to customize your User Model as mentioned in the documentation

Django: 'no such table' after extending the User model using OneToOneField

(Django 1.10.) I'm trying to follow this advice on extending the user model using OneToOneField. In my app 'polls' (yes, I'm extending the app made in the 'official' tutorial) I want to store two additional pieces of information about each user, namely, a string of characters and a number.
In my models.py I now have the following:
from django.contrib.auth.models import User
class Employee(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
stopien = models.CharField(max_length=100)
pensum = models.IntegerField()
and in admin.py the following:
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from polls.models import Employee
class EmployeeInline(admin.StackedInline):
model = Employee
can_delete = False
verbose_name_plural = 'employee'
class UserAdmin(BaseUserAdmin):
inlines = (EmployeeInline, )
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
When adding a user using the admin panel my two new fields display correctly. However, when I click 'save', or if I don't add any user and just click on the name of my sole admin user in the admin panel, I get the following error:
OperationalError at /admin/auth/user/1/change/
no such table: polls_employee
I see some questions and answers related to similar problems, but they seem to be relevant for older version of Django. Could anyone give me a tip as to what I should do? Ideally I'd want my two additional fields display in the admin panel, though I suspect this might be a task for the future.
I have to confess I do not understand this paragraph from the documentation just following the advice I'm using:
These profile models are not special in any way - they are just Django models that happen to have a one-to-one link with a User model. As such, they do not get auto created when a user is created, but a django.db.models.signals.post_save could be used to create or update related models as appropriate.
Do I need to tie this 'post-save' to some element of the admin panel?
I'd be very greatful for any help!
You need run makemigrations to create a migration for your new model, and then migrate to run the migration and create the database table.
./manage.py makemigrations
./manage.py migrate

Django User belongs to Company

I am really new to Django, I would like to have users, that belong to a company, so many users to a single company. Do I need to copy the existing user model and add to my project? Where would I find the User model to extend?
Sorry if this is not very descriptive it is my first project with python and django.
(If you need many companies to one user) you don't need to copy the user model. Just create a "Company" model and use "ForeignKey".
Example:
from django.contrib.auth import get_user_model
User = get_user_model()
class Company(models.Model):
user = models.ForeignKey(User)
Opposite(If you need many users to one company):
#settings.py
AUTH_USER_MODEL = 'myapp.User'
#myapp.models.py
from django.contrib.auth.models import User as BaseUser, UserManager
class User(BaseUser):
company = models.ForeignKey(Company)
# Use UserManager to get the create_user method, etc.
objects = UserManager()

When to use the Custom User Model in Django 1.5

I have a question regarding the custom user model in Django 1.5
So right now the default user model looks just fine to me, I just need to add a few other variables such as gender,location and birthday so that users can fill up those variables after they have successfully registered and activated their account.
So, what is the best way to implement this scenario?
Do I have to create a new app called Profile and inherit AbstractBaseUser? and add my custom variable to models.py? Any good example for me to follow?
thank you in advance
You want to extend your user model to the AbstractUser and add your additional fields. AbstractUser inherits all of the standard user profile fields, whereas AbstractBaseUser starts you from scratch without any of those fields.
It's hard to define best practices this close to the release, but it seems that unless you need to drastically redefine the User model, then you should use AbstractUser where possible.
Here are the docs for extending the User model using AbstractUser
Your models.py would then look something like this:
class MyUser(AbstractUser):
gender = models.DateField()
location = models.CharField()
birthday = models.CharField()
MyUser will then have the standard email, password, username, etc fields that come with the User model, and your three additional fields above.
Then you need to add the AUTH_USER_MODEL to your settings.py:
AUTH_USER_MODEL = 'myapp.MyUser'

inheritance from the django user model results in error when changing password

I inherited form the django user model like so:
from django.db import models
from django.contrib.auth.models import User, UserManager
from django.utils.translation import ugettext_lazy as _
class NewUserModel(User):
custom_field_1 = models.CharField(_('custom field 1'), max_length=250, null=True, blank=True)
custom_field_2 = models.CharField(_('custom field 2'), max_length=250, null=True, blank=True)
objects = UserManager()
When i go to the admin and add an entry into this model, it saves fine, but below the "Password" field where it has this text "Use '[algo]$[salt]$[hexdigest]' or use the change password form.", if i click on the "change password form' link, it produces this error
Truncated incorrect DOUBLE value: '7/password'
What can i do to fix this?
The best way to extend Django's User model is to create a new Profile model and identify it through the AUTH_PROFILE_MODULE setting. See http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/, and http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
This adds a get_profile() method to User instances which retrieves your associated model for a given User.
While doable (I did it once and regret it) using inheritance to extend the User model is not the best idea. I'd suggest you take Chris' advice and extend the User model with 1-1 relationship as it is the "standard" and "supported" way of doing it, and the way reusable apps deal with user profiles. Otherwise you need to implement an authentication backend if you want to do it by inheritance. So if you MUST do it see this. But be warned, you'll stumble across other problems later.