How do I create user registration for a AbstractUser? - django

Since Django recommends having a custom user model for a new project, I've extended the AbstractUser model without adding any additional fields, but how do I create a signup page for new users with my new user model?
My model:
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
pass

After creating customer model you have to build registration form.
you can check out this video he explained it well.
Also you can add additional fields
AbstractBaseUser and UserCreationForm

Related

Django admin: created users have plain text password and can't login

I want to have a custom tab for staff users so in admin.py I do:
class StaffUser(User):
class Meta:
proxy = True
#admin.register(StaffUser)
class AdminUserAdmin(admin.ModelAdmin):
pass
But on the admin site, whenever I add a new user with this interface I can't log in with it since for some reason it sets it's password as plain text instead of hashing it.
I've read this post BUT if I do that and change StaffUser to inherint from AdminUserI get this other error
AttributeError: type object 'StaffUser' has no attribute '_meta'
You should declare your models in models.py (or a models package), not admin.py.
UserAdmin (there is no such thing as an AdminUser in the Django packages) is a subclass of ModelAdmin, that is, a class for registering models in the Django admin. It is not a model subclass that you can extend to make new models.
Also, if you are trying to extend the User just to distinguish users with access to the Django admin site, note that there is already an is_staff property in the default User model. And in any case, if you still want to extend the User model, you should be extending AbstractUser instead as stated in the docs.

How to extend a user model without use profile method?

I want to add a column to django auth_user table. But I do not want to create a new table.
As the document peovided for extending User model, there are four ways.
proxy model
Add a oneToOne field
Creating a Custom User Model Extending AbstractUser
Creating a Custom User Model Extending AbstractBaseUser
It works some time. But How could I add a field to the auth user model it provied?

Django user with extra fields- extending User

Hey I'm new to allauth package.
I would like to know how to write a user model that extends the all auth user.
for example i would like to have a user that has allauth user fields in it and also an image, list of favorite text field.
I would realy appreciate any help!!
You can extend the Django user model using an OneToOneField(User).
Here's the relevant part of the Django docs: Extending the existing User model
IF you need it you can use a custom user model but this will be more difficult. In this case see allauth custom user models and Django documentation
Since you want allauth user fields with some extra fields, you can inherit default user model and add your custom fields to it.
from django.contrib.auth.models import AbstractUser
class ExtendedUser(AbstractUser):
favourites = models.charField(max_length=255)
....
....
In your settings.py file, add this line
AUTH_USER_MODEL = 'users.ExtendedUser'
To store the extra fields when user signs up, use django-allauth signals as in your views as follows.
from django.dispatch import receiver
from allauth.account.signals import user_signed_up
#receiver(user_signed_up)
def user_signed_up(request, user, **kwargs)
#get extra fields from kwargs and save to database

want to extend auth_user model in django by adding two fields

in django,i want to extend the auth_user model and adding the 2 fields.one is created_user which will display the date and time when user created something and other is modified_user which will display the date n time when modification is done..
is it possible by migration??
i ve tried dis code..
from django.contrib.auth.models import User, UserManager
class CustomUser(User):
created_user= models.DateTimeField("date and time when created")
modified_user=models.DateTimeField("date and time when modified")
objects= UserManager()
I suggest reading the documentation on creating your own custom user model.
In your particular case, the easiest thing would probably be to subclass AbstractUser and add your fields as above.
If you’re entirely happy with Django’s User model and you just want to add some additional profile information, you can simply subclass django.contrib.auth.models.AbstractUser and add your custom profile fields. This class provides the full implementation of the default User as an abstract model.

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

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