英文:
How to Modify the default Django user table
问题
如何修改默认的Django用户表以及自定义的用户表应包括'first name','last name','gender','Email(Primary Key)','phone number'字段。如何做到这一点?
forms.py
class UserForm(ModelForm):
class Meta():
model = User
fields = 'first_name', 'last_name', 'gender', 'email', 'phone_number'
英文:
How to Modify the default Django user table and the custom user table should consist of the 'first name','last name', 'gender', 'Email(Primary Key)', 'phone number' fields. How to do this?
forms.py
class UserForm(ModelForm):
class Meta():
model= User
fields = 'First_name','Last_name','Gender', 'email','Phone_number'
答案1
得分: 3
你需要创建自定义模型来扩展Django用户模型。请查看[文档][1]。
使用您的字段的示例
from django.contrib.auth.models import User
class ExtendedUser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
First_name = models.CharField(max_length=100)
Last_name = models.CharField(max_length=100)
Gender = models.CharField(max_length=100)
email = models.CharField(max_length=100)
Phone_number = models.CharField(max_length=100)
然后在您的表单中使用它。
[1]: https://docs.djangoproject.com/en/3.0/topics/auth/customizing/#extending-the-existing-user-model
英文:
You need to create custom model to extend Django user model.
look in the [documentation][1]
Example with your fields
from django.contrib.auth.models import User
class ExtendedUser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
First_name = models.CharField(max_length=100)
Last_name= models.CharField(max_length=100)
Gender = models.CharField(max_length=100)
email = models.CharField(max_length=100)
Phone_number = models.CharField(max_length=100)
and then use it in your form
[1]: https://docs.djangoproject.com/en/3.0/topics/auth/customizing/#extending-the-existing-user-model
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论