[英]How to add 'mobile' field to default auth_user table in User model in django?
I want to add Mobile filed to be saved to auth_user table when i register.我想在注册时添加要保存到 auth_user 表的移动文件。
models.py模型.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表格.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.如果要将所有用户数据存储在一起,则应替换用户模型,而不是创建 OneToOne 关系。 Judging by the current code you will get 2 tables - one for standard Django user and one connected to it with mobile data.根据当前代码判断,您将获得 2 个表 - 一个用于标准 Django 用户,另一个通过移动数据连接到它。
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 在 Django 中使用自定义字段扩展用户模型
Or directly in the documentation:或者直接在文档中:
https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model 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这样做并输入终端 python manage.py makemigrations python manage.py migrate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.