我想添加Mobile字段,以便在注册时保存到auth_user表中。
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']
答案 0 :(得分:1)
如果要将所有用户数据存储在一起,则应替换用户模型,而不要创建OneToOne关系。从当前代码来看,您将获得2张表-一张表供标准Django用户使用,另一张表与移动数据连接。
在这里,您可以了解有关替换用户以及这两种方法之间的区别的更多信息:
Extending the User model with custom fields in Django
或直接在文档中
https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model
答案 1 :(得分:0)
Click here for the first image
Click here for the second image
这样做并在终端中输入这些代码
python manage.py makemigrations
python manage.py migrate