【问题标题】:Can't I add some columns to User model of django.contrib.auth.models?我不能在 django.contrib.auth.models 的用户模型中添加一些列吗?
【发布时间】:2017-11-23 01:42:31
【问题描述】:

我想在 django.contrib.auth.models 的用户模型中添加生日和性别数据列。但是,我写了 forms.py 之类的

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.forms import AuthenticationForm
from .models import User

class RegisterForm(UserCreationForm):
    class Meta:
        model = User
        fields = ('username', 'email','password1','password1','birthday','sex',)
    def __init__(self, *args, **kwargs):
        super(RegisterForm, self).__init__(*args, **kwargs)
        self.fields['username'].widget.attrs['class'] = 'form-control'
        self.fields['email'].widget.attrs['class'] = 'form-control'
        self.fields['password1'].widget.attrs['class'] = 'form-control'
        self.fields['password2'].widget.attrs['class'] = 'form-control'
        self.fields['birthday'].widget.attrs['class'] = 'form-control'
        self.fields['sex'].widget.attrs['class'] = 'form-control'

当我运行代码时,django.core.exceptions.FieldError: Unknown field(s) (birthday) specified for User 错误发生。 我搜索了 Django 文档,所以我发现各种用户对象的字段是有限的,比如只有用户名、电子邮件和密码、is_staff 和 last_login 等。但是现在我想添加生日和性别数据列,我该怎么做?不能我做吗?我应该怎么写? 现在通过一个答案,我重写了models.py

from django.db import models
from django.contrib.auth.models import User

class NewUser(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    birthday = models.DateField()
    sex = models.IntegerField()

我重写了forms.py

class RegisterForm(ModelForm):
    class Meta:
        model = NewUser
        fields = ('username', 'email','password1','password1','birthday',)
    def __init__(self, *args, **kwargs):
        super(RegisterForm, self).__init__(*args, **kwargs)
        self.fields['username'].widget.attrs['class'] = 'form-control'
        self.fields['email'].widget.attrs['class'] = 'form-control'
        self.fields['password1'].widget.attrs['class'] = 'form-control'
        self.fields['password2'].widget.attrs['class'] = 'form-control'
        self.fields['birthday'].widget.attrs['class'] = 'form-control'

我用同样的方法重写了forms.py 之前。但是我得到了一个错误django.core.exceptions.FieldError: Unknown field(s) (username, password1, email) specified for NewUser。我应该如何解决这个问题?

【问题讨论】:

    标签: python django


    【解决方案1】:

    更新

    在您的RegisterForm 中,model 应该是NewUser,您可能想要使用ModelForm

    Creating forms from models

    原件

    只改form是不够的,你需要从model开始,我建议你用OneToOneField加你自己的model

    from django.contrib.auth.models import User
    
    class Employee(models.Model):
        user = models.OneToOneField(User, on_delete=models.CASCADE)
        birthday = models.DateField()
        sex = models.IntegerField()
    

    然后您可以创建自己的form

    Extending the existing User model

    【讨论】:

    • 谢谢你的回答。我更新了我的问题,如果你知道什么请帮助我
    • thx。但同样的错误发生了。我再次更新了我的问题。你能检查我的代码与你的预期代码相同吗?
    • @user8817477 username, password1, email 这三个不应该在fields,因为它们不在NewUser,你可以在RegisterForm 字段中添加它们。
    • 我无法理解如何编写你的代码。如何将用户名、密码1、电子邮件添加到 RegisterForm 字段?你能在你的答案中写下你想要的代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    • 2011-06-17
    相关资源
    最近更新 更多