У меня есть ModelForm с некоторыми поддельными полями, которые я обрабатываю позже, когда сохраняю модель.Django скрыть поддельное поле в форме модели
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)
Что мне нужно, чтобы показать или скрыть некоторые из них в зависимости от того, являются ли мы добавлять новый экземпляр или изменить существующий.
Все, что я прочитал, это скрыть реальные поля, которые определены в модели, но это не мое дело. С «поддельными» я имею в виду поля, которые не находятся в модели или базе данных, только определенные в форме.
Я попытался это в __init__
переопределении в ModelForm:
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()'? – C14L
Извините за мой короткий вопрос. Я уже редактировал. Поддельное поле - это поле, которое находится только в форме, а не в таблице модели или базы данных. То, что я пробовал, после super(). – toscanelli