1
這裏是否存在等是我的模型:的Django無法檢測模型的現場
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和遷移命令,這樣在我的數據庫表polls_question,應該在shcema主鍵名爲id我的後端SQLite數據庫更新。
你註冊了admin.site.register(QuestionAdmin)這樣的管理字段嗎?遷移沒有錯誤? – hansTheFranz
@hansTheFraz我相信它的admin.site.register(Question,QuestionAdmin)?和遷移不會給我任何錯誤 – paradox
@paradox爲什麼你想改變'問題'的ID?我相信這是你的問題。你不能只改變一個對象的ID。 –