How to extend a user model without use profile method? - django

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?

Related

How do I create user registration for a AbstractUser?

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

How to extend django admin user model with new fields?

I want to add more fields to django built in admin user model. How can I achieve it?
Actually , There are three users on django server, student , teacher and parent. And by adding a 'role' field to user model, I can identify which type of user is log-ged in . So how can I extend the model field.
I'm expecting to identify different role of users in login.

Custom Django Authentication

I have an model named Customers(username,password ..etc) and also an model named User(username,password...etc).
I want to create two different APIs with different authentication.
One should authenticate with the User username,password
and the second should authenticate using the Customers username,password.
Any idea on how can I do this?
Thank you!
I suggest the following options:
1.
I am assuming User model is the "real" user of your app. If this is true use the django's default User model class. It will work out of the box.
For the Customer model, make it inherit from AbstractBaseUser, this will give you password functionality out of the box and you can add other fields as per your need.
Now you can create 2 different urls for login. 1 url for user which checks in the User model and the other for the customer model. This avoids any confusion for everyone.
If you prefer a single url, you have to mention the model class along with username and password to know in which table to verify them.
2.
Create two profile models: UserProfile and CustomerProfile
Each will have a one to one relationship with the django's default User model.
Basically a User can have the profile of a "real" user or of a customer.
In this case when you are creating any User you have check if you want to attach a UserProfile or a CustomerProfile.
In this case it makes sense to just use a single login url. From the user's login information you can first fetch the user from the User table and then check if it is a customer or not by running a query in the CustomerProfile table.
I recommend you to use the django.contrib.auth.user class for your classical authentication. You can either inherit from that class or add a OneToOne relation to your own model as follows
from django.contrib.auth.models import User
class YourUser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
For the rest of your question you should add some more details and even some pieces of your code.

How make Django's auth app use my custom model "User"

Hello I am very new to Django, and I am making an app that stores a lot of information about the user. But django's auth app stores only a bit of information about the user. Is there any way I can create my own model "User" and make Django's auth app use this model. Thanks.
This has been supported since Django 1.5, and is fully covered in the documentation.
Basically, you need to subclass auth.models.AbstractUser, and set the AUTH_USER_MODEL setting to point to your new model.
You can solves your problem with UserProfile model. And you can store the user extra information in this with relation of onetone or ForeignKey with unique property field.
Django user profile
U can multi-table inheritance the user model
from django.contrib.auth import User
class MyUser(User):
//add ur required fields
Reference for in heritance.

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.