【问题标题】:Django hide a fake field in a modelformDjango在模型表单中隐藏一个假字段
【发布时间】:2016-10-10 00:55:44
【问题描述】:

我有一个带有一些假字段的 ModelForm,我稍后会在保存模型时处理这些字段。

class MyForm(forms.ModelForm):
    field1= forms.IntegerField(min_value=0)
    field2= forms.ChoiceField(choices=SIDES_CHOICES, required=True)
    field3= forms.CharField(max_length=140, required=False)

我需要根据我们是添加新实例还是更改现有实例来显示或隐藏其中一些。

我读到的只是关于隐藏模型中定义的真实字段,但这不是我的情况。 “假”是指不在模型或数据库中的字段,仅在表单中定义。

我已经在 ModelForm 的 __init__ 覆盖中尝试过这个:

def __init__(self, *args, **kwargs):
    super(MyForm, self).__init__(*args, **kwargs)
    if not self.instance.pk:
        del self.fields['field1']

但我收到此错误:

Key 'field1' not found in 'MyForm'

整个错误回溯:

Template error:
In template C:\Python34\lib\site-packages\django_suit-0.2.18-py3.4.egg\suit\templates\admin\change_form.html, error at line 19
   Key 'field1' not found in 'MyForm'   9 :   {{ media }}
   10 : 
   11 : {% endblock %}
   12 : 
   13 : 
   14 : {% block extrajs %}
   15 :   {{ block.super }}
   16 : 
   17 :   {% if 'CONFIRM_UNSAVED_CHANGES'|suit_conf %}
   18 :     <!-- Warn on leaving unsaved form -->
   19 :     <script src="{% static  'suit/js/suit-form- confirm.js' %}"></script>
   20 :     <script type="text/javascript">
   21 :       confirmExitIfModified('{% firstof opts.model_name opts.module_name %}_form', '{% trans 'You have unsaved changes' %}.');
   22 :     </script>
   23 :   {% endif %}
   24 : 
   25 :   {% if adminform.model_admin.suit_form_tabs %}
   26 :     <script type="text/javascript">
   27 :     (function ($) {
   28 :       $(function () {
   29 :         $('#suit_form_tabs').suit_form_tabs();


Traceback:

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\forms\forms.py" in __getitem__
  141.             field = self.fields[name]

During handling of the above exception ('field1'), another exception occurred:

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\core\handlers\base.py" in get_response
  174.                     response = self.process_exception_by_middleware(e, request)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\core\handlers\base.py" in get_response
  172.                     response = response.render()

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\response.py" in render
  160.             self.content = self.rendered_content

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\response.py" in rendered_content
  137.         content = template.render(context, self._request)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\backends\django.py" in render
  95.             return self.template.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render
  206.                     return self._render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in _render
  197.         return self.nodelist.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\loader_tags.py" in render
  173.         return compiled_parent._render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in _render
  197.         return self.nodelist.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\loader_tags.py" in render
  173.         return compiled_parent._render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in _render
  197.         return self.nodelist.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\loader_tags.py" in render
  69.                 result = block.nodelist.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\loader_tags.py" in render
  69.                 result = block.nodelist.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render
  1043.             output = self.filter_expression.resolve(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in resolve
  709.                 obj = self.var.resolve(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in resolve
  850.             value = self._resolve_lookup(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in _resolve_lookup
  913.                             current = current()

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\loader_tags.py" in super
  83.             return mark_safe(self.render(self.context))

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\loader_tags.py" in render
  69.                 result = block.nodelist.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\loader_tags.py" in render
  69.                 result = block.nodelist.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\loader_tags.py" in render
  69.                 result = block.nodelist.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\defaulttags.py" in render
  220.                     nodelist.append(node.render_annotated(context))

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\loader_tags.py" in render
  209.                 return template.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render
  208.                 return self._render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in _render
  197.         return self.nodelist.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\defaulttags.py" in render
  220.                     nodelist.append(node.render_annotated(context))

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\defaulttags.py" in render
  584.             return self.nodelist.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\defaulttags.py" in render
  319.                     match = condition.eval(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\defaulttags.py" in eval
  951.         return self.value.resolve(context, ignore_failures=True)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in resolve
  709.                 obj = self.var.resolve(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in resolve
  850.             value = self._resolve_lookup(context)

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\template\base.py" in _resolve_lookup
  913.                             current = current()

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\contrib\admin\helpers.py" in errors
  117.             for f in self.fields if f not in self.readonly_fields).strip('\n')

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\contrib\admin\helpers.py" in <genexpr>
  117.             for f in self.fields if f not in self.readonly_fields).strip('\n')

File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\forms\forms.py" in __getitem__
  144.                 "Key %r not found in '%s'" % (name, self.__class__.__name__))

【问题讨论】:

  • 您需要提供更多信息。什么是“假场”,它与真实场有何不同?首先,您是如何定义这些字段的?
  • 调用super()之前还是之后?
  • 抱歉我的简短问题。我已经编辑过了。假字段是只存在于表单中的字段,不在模型或数据库表中。我尝试的是在 super() 之后。
  • 将额外字段添加到Meta 类中的fields 列表中。
  • @evergreen 他们已经在里面了,但是我删除了我不想在__init__方法中显示的内容,看来django不喜欢这样...

标签: django modelform


【解决方案1】:

如果表单用于模型的新实例,您将从__init__ 中的表单中删除field1。关键错误意味着您的 ModelForm 代码中至少有一个地方在您删除它之后引用了field1(对于新实例)。如果错误回溯没有将您指向产生错误的行,只需在您的代码中搜索field1 的出现情况,并确保在执行每个代码部分之前测试新实例。

编辑 由于该表单被用作 AdminForm,请注释掉 ModelForm 中删除 field1 的行,并根据对 Remove fields from ModelForm 的回答将以下内容添加到您的 ModelAdmin:

def get_form(self, request, obj=None, **kwargs):
    form = super().get_form(request, obj=obj, **kwargs)
    if not obj:
        self.exclude = ['field1',]
    return form

【讨论】:

  • 错误来自这一行:C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\forms\forms.py in __getitem__, line 144我没有在我的代码的任何其他部分(有意识地)引用这个字段。
  • 能否请您发布整个错误回溯?你可以编辑你原来的问题。
  • 感谢您的建议。完成!
  • 错误显然来自管理员。您的 ModelAdmin 中是否有对 field1 的引用?
  • 没有人。这些字段不得显示在更改列表中,除了 list_display 之外,我没有任何其他字段列表。在 list_display 中,我只有模型中的字段。
猜你喜欢
  • 2010-12-11
  • 1970-01-01
  • 2011-09-13
  • 2011-01-06
  • 2011-06-08
  • 2016-05-31
  • 1970-01-01
  • 1970-01-01
  • 2011-10-08
相关资源
最近更新 更多