django admin fieldsets上的FieldError

时间:2016-05-19 16:25:35

标签: python django

我从现在开始几个月尝试Django,我面临着一种奇怪的行为。管理模型类不起作用。但是,字段名称似乎很好,代码在没有 fieldsets 变量的情况下正常工作。

这是模型类:

class Lunch_menu(models.Model):
   lunch_date = models.DateField(auto_now=True)

   midday_entrance = models.CharField(max_length=256)
   midday_dish = models.CharField(max_length=256)
   midday_dessert = models.CharField(max_length=256)

   dinner_entrance = models.CharField(max_length=256)
   dinner_dish = models.CharField(max_length=256)
   dinner_dessert = models.CharField(max_length=256)

ModelAdmin:

from django.contrib import admin
from self_menu.models import Lunch_menu

class LunchAdmin(admin.ModelAdmin):
   list_display = ('lunch_date', 'midday_dish', 'dinner_dish')
   fieldsets = (
       (None, {
           'fields': ('lunch_date',)
       }),
       ('Lunch', {
           'classes': ('collapse'),
           'fields': ('midday_entrance', 'midday_dish', 'midday_dessert'),
       }),
   )

admin.site.register(Lunch_menu, LunchAdmin)

最后是错误代码:

Internal Server Error: /admin/self_menu/lunch_menu/2/change/
Traceback (most recent call last):
  File "/usr/lib/python3.5/site-packages/django/contrib/admin/options.py", line 639, in get_form
    return modelform_factory(self.model, **defaults)
  File "/usr/lib/python3.5/site-packages/django/forms/models.py", line 545, in modelform_factory
    return type(form)(class_name, (form,), form_class_attrs)
  File "/usr/lib/python3.5/site-packages/django/forms/models.py", line 257, in __new__
    raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (lunch_date) specified for Lunch_menu

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/lib/python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/lib/python3.5/site-packages/django/contrib/admin/options.py", line 541, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/usr/lib/python3.5/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/usr/lib/python3.5/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/usr/lib/python3.5/site-packages/django/contrib/admin/sites.py", line 244, in inner
    return view(request, *args, **kwargs)
  File "/usr/lib/python3.5/site-packages/django/contrib/admin/options.py", line 1440, in change_view
    return self.changeform_view(request, object_id, form_url, extra_context)
  File "/usr/lib/python3.5/site-packages/django/utils/decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "/usr/lib/python3.5/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/usr/lib/python3.5/site-packages/django/utils/decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/usr/lib/python3.5/contextlib.py", line 30, in inner
    return func(*args, **kwds)
  File "/usr/lib/python3.5/site-packages/django/contrib/admin/options.py", line 1367, in changeform_view
    ModelForm = self.get_form(request, obj)
  File "/usr/lib/python3.5/site-packages/django/contrib/admin/options.py", line 642, in get_form
    % (e, self.__class__.__name__))
django.core.exceptions.FieldError: Unknown field(s) (lunch_date) specified for Lunch_menu. Check fields/fieldsets/exclude attributes of class LunchAdmin.

1 个答案:

答案 0 :(得分:0)

您为auto_now=True设置了lunch_date,这就是导致错误的原因。 将lunch_date设为只读或删除auto_now设置