I want to add Mobile filed to be saved to auth_user table when i register.
models.py
from django.db import models
from django.contrib.auth.models import User
class UserRegModel(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
mobile = models.CharField(max_length=100)
forms.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
class UserRegisterForm(UserCreationForm):
mobile = forms.CharField(max_length=15)
class Meta:
model = User
fields = ['username','email','mobile','password1','password2']
If you want to store all user data together, you should substitute a user model instead of creating a OneToOne relationship. Judging by the current code you will get 2 tables - one for standard Django user and one connected to it with mobile data.
Here you can read more about substituting a user and the difference between these 2 approaches:
Extending the User model with custom fields in Django
Or directly in the documentation:
https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model
enter image description here enter image description here
do this and type in terminal python manage.py makemigrations python manage.py migrate
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.