gpt4 book ai didi

python - Django 无法检测模型字段的存在

转载 作者:太空宇宙 更新时间:2023-11-03 14:57:32 27 4
gpt4 key购买 nike

这是我的模型:

class Question(models.Model):
id = models.AutoField(primary_key=True)
question_title = models.CharField(max_length=100, default="no title")
question_text = models.CharField(max_length=200, default ="no content")

def __unicode__(self):
return self.question_title + self.question_text

在我的管理配置页面中,我设置了 QuestionAdmin 类,如下所示:

class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
('Question ID', {'fields':['id_pk']}),
('Question content',{'fields':['question_title',"question_text"]})
]

然后我将此配置应用到 django 的管理页面:

admin.site.register(Question, QuestionAdmin)

这是我的完整错误跟踪:

Environment:


Request Method: GET
Request URL: http://www.whiletrue.cc/paradox/admin/polls/question/add/

Django Version: 1.11.2
Python Version: 2.7.12
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls.apps.PollsConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/usr/local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)

File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
551. return self.admin_site.admin_view(view)(*args, **kwargs)

File "/usr/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
149. response = view_func(request, *args, **kwargs)

File "/usr/local/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
57. response = view_func(request, *args, **kwargs)

File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
224. return view(request, *args, **kwargs)

File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in add_view
1508. return self.changeform_view(request, None, form_url, extra_context)

File "/usr/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
67. return bound_func(*args, **kwargs)

File "/usr/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
149. response = view_func(request, *args, **kwargs)

File "/usr/local/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
63. return func.__get__(self, type(self))(*args2, **kwargs2)

File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in changeform_view
1408. return self._changeform_view(request, object_id, form_url, extra_context)

File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in _changeform_view
1437. ModelForm = self.get_form(request, obj)

File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in get_form
651. % (e, self.__class__.__name__)

Exception Type: FieldError at /paradox/admin/polls/question/add/
Exception Value: Unknown field(s) (id) specified for Question. Check fields/fieldsets/exclude attributes of class QuestionAdmin.

我已经使用manage.py makemigrations和migrate命令更新了后端sqlite数据库,因此在我的数据库表polls_question中,shcema中应该有一个名为id的主键。

最佳答案

问题是 id 字段无法更改,必须呈现为只读字段。以下是如何执行此操作的示例:

class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
('Question ID', {'fields':['id']}),
('Question content',{'fields':['question_title',"question_text"]})
]
readonly_fields = ('id', )

您必须在 ModelAdmin 类的 readonly_fields 属性中定义它,该属性必须是列表或元组。

关于python - Django 无法检测模型字段的存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45406345/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com