如何修改默认的Django用户表

huangapple go评论63阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年1月4日 00:22:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定