Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用户模型的Django模型表单中存在未知字段错误_Python_Django - Fatal编程技术网

Python 用户模型的Django模型表单中存在未知字段错误

Python 用户模型的Django模型表单中存在未知字段错误,python,django,Python,Django,这就是我正在尝试的 from django import forms from django.contrib.auth.models import User class UserForm(forms.ModelForm): class Meta: model=User fields=(' electricity','natural_gas','heating_oil','coal','lpg','propane','wooden_pellets') 这是错误代码 rai

这就是我正在尝试的

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

class UserForm(forms.ModelForm):

class Meta:

    model=User

    fields=(' electricity','natural_gas','heating_oil','coal','lpg','propane','wooden_pellets')
这是错误代码

raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (propane, heating_oil, wooden_pellets, lpg, coal,  electricity, natural_gas) specified for User

您正在此处导入django给定的用户

from django.contrib.auth.models import User
它有以下字段

id
password
last_login
is_superuser
username
first_name
last_name
email
is_staff
is_active
date_joined
在元类内部的
UserForm
中,您将模型指定为用户并尝试访问字段

fields=(' electricity','natural_gas','heating_oil','coal','lpg','propane','wooden_pellets')
很明显,这是不存在的

也许你想参考其他的模型。如果您继承自django
user
model类,则还可以使用上述字段创建您喜欢的用户,如:

class MyUser(User):
    electricity= models.CharField(max_length=20)
    #your fields here

并在UserForm的meta中指定为
model=MyUser

嗨,欢迎使用SO!能否添加一些上下文,生成
字段错误的代码是什么,提供更多的上下文:
Meta
fields
param中提到的@sohail字段不属于
django.contrib.auth.models.User
如果您想在表单中添加非模型字段,可以像
electricity=forms.IntegerField()
那样添加它们,您在模型中在哪里定义了这些字段?默认的
User
模型没有这些字段,因此表单将无法与
model=User
一起使用。