我整晚都在寻找这个问题的答案,但似乎找不到问题所在。
我正在将自定义 CMS 移植到 Django,基本上它具有以下结构:
- 一个条目有 1 个布局
- 一个布局有很多部分
因此,在用户创建并输入之后,当他们编辑它时,我想显示该布局中包含的所有部分。
所以我从条目中获取布局 ID,循环并获取部分,并尝试将它们添加到表单中。
当我调试它时,看起来一切都被添加到字段对象中,但它不会显示在表单上,除非我预定义字段,这会破坏目的
我在这里遵循了几个示例并获得了以下代码:
class EntryChangeForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(EntryChangeForm, self).__init__(*args, **kwargs)
layout = Layout.objects.filter(id=self.instance.layout_id)
layout_sections = Section.objects.filter(layout_id=layout)
for section in layout_sections:
self.fields['section_%d' % section.id] = models.CharField(max_length=200, verbose_name=section.section_label)
current_section = Data.objects.filter(page_id=self.instance.id, section_id=section.id, content_table_id=2)
if current_section:
self.fields['section_%d' % section.id.initla] = current_section.text
else:
self.fields['section_%d' % section.id].initial = ""
我调试和打印 self.fields 的示例
{'layout':
<django.forms.models.ModelChoiceField object at 0x1c30b50>,
'uid': <django.forms.fields.IntegerField object at 0x1c30c50>,
'url': <django.forms.fields.CharField object at 0x1c30cd0>,
'url_301': <django.forms.fields.CharField object at 0x1c30d50>,
'name': <django.forms.fields.CharField object at 0x1c30dd0>,
'page_title': <django.forms.fields.CharField object at 0x1c30e50>,
'meta_description':
<django.forms.fields.CharField object at 0x1c30ed0>,
'meta_keywords': <django.forms.fields.CharField object at 0x1c30f50>,
'order_fld': <django.forms.fields.CharField object at 0x1c30fd0>,
'user_id': <django.forms.fields.IntegerField object at 0x1c32090>,
'author': <django.forms.models.ModelChoiceField object at 0x1c32110>,
'date_active': <django.forms.fields.SplitDateTimeField object at 0x1c32210>,
'date_added': <django.forms.fields.SplitDateTimeField object at 0x1c32290>, 'date_modified': <django.forms.fields.SplitDateTimeField object at 0x1c32390>, 'date_expires': <django.forms.fields.SplitDateTimeField object at 0x1c32490>, 'date_published': <django.forms.fields.SplitDateTimeField object at 0x1c32590>, 'is_active': <django.forms.fields.TypedChoiceField object at 0x1c32690>, 'hide_from_dropdown': <django.forms.fields.TypedChoiceField object at 0x1c32790>, 'is_featured':
<django.forms.fields.TypedChoiceField object at 0x1c32810>,
'in_sitemap': <django.forms.fields.TypedChoiceField object at 0x1c32890>,
'admin_user_id': <django.forms.fields.IntegerField object at 0x1c32910>, 'show_large_photo': <django.forms.fields.TypedChoiceField object at 0x1c32990>, 'is_featured_on_homepage': <django.forms.fields.TypedChoiceField object at 0x1c32a10>, 'capitalize_first_letter': <django.forms.fields.TypedChoiceField object at 0x1c32a90>, 'display_share_box': <django.forms.fields.TypedChoiceField object at 0x1c32b10>, 'display_subscribe_box': <django.forms.fields.TypedChoiceField object at 0x1c32b90>, 'is_commenting_enabled': <django.forms.fields.TypedChoiceField object at 0x1c32c10>, 'legacy_import':
<django.forms.fields.TypedChoiceField object at 0x1c32c90>,
'section': <django.db.models.fields.CharField>,
'section_3': <django.db.models.fields.CharField>,
'section_2': <django.db.models.fields.CharField>,
'section_4': <django.db.models.fields.CharField>}
{'layout':
<django.forms.models.ModelChoiceField object at 0x1c30b50>,
'uid': <django.forms.fields.IntegerField object at 0x1c30c50>,
'url': <django.forms.fields.CharField object at 0x1c30cd0>,
'url_301': <django.forms.fields.CharField object at 0x1c30d50>,
'name': <django.forms.fields.CharField object at 0x1c30dd0>,
'page_title': <django.forms.fields.CharField object at 0x1c30e50>,
'meta_description': <django.forms.fields.CharField object at 0x1c30ed0>,
'meta_keywords': <django.forms.fields.CharField object at 0x1c30f50>,
'order_fld': <django.forms.fields.CharField object at 0x1c30fd0>,
'user_id': <django.forms.fields.IntegerField object at 0x1c32090>,
'author': <django.forms.models.ModelChoiceField object at 0x1c32110>,
'date_active': <django.forms.fields.SplitDateTimeField object at 0x1c32210>,
'date_added': <django.forms.fields.SplitDateTimeField object at 0x1c32290>, 'date_modified': <django.forms.fields.SplitDateTimeField object at 0x1c32390>, 'date_expires': <django.forms.fields.SplitDateTimeField object at 0x1c32490>, 'date_published': <django.forms.fields.SplitDateTimeField object at 0x1c32590>, 'is_active': <django.forms.fields.TypedChoiceField object at 0x1c32690>, 'hide_from_dropdown': <django.forms.fields.TypedChoiceField object at 0x1c32790>, 'is_featured': <django.forms.fields.TypedChoiceField object at 0x1c32810>,
'in_sitemap': <django.forms.fields.TypedChoiceField object at 0x1c32890>,
'admin_user_id': <django.forms.fields.IntegerField object at 0x1c32910>, 'show_large_photo': <django.forms.fields.TypedChoiceField object at 0x1c32990>, 'is_featured_on_homepage': <django.forms.fields.TypedChoiceField object at 0x1c32a10>, 'capitalize_first_letter': <django.forms.fields.TypedChoiceField object at 0x1c32a90>, 'display_share_box': <django.forms.fields.TypedChoiceField object at 0x1c32b10>, 'display_subscribe_box': <django.forms.fields.TypedChoiceField object at 0x1c32b90>, 'is_commenting_enabled': <django.forms.fields.TypedChoiceField object at 0x1c32c10>, 'legacy_import': <django.forms.fields.TypedChoiceField object at 0x1c32c90>,
'section': <django.db.models.fields.CharField>,
'section_3': <django.db.models.fields.CharField>,
'section_2': <django.db.models.fields.CharField>,
'section_4': <django.db.models.fields.CharField>}
我可以看到我添加的字段与模型中已经存在的字段的明显差异:'legacy_import': <django.forms.fields.TypedChoiceField object at 0x1c32c90>, 'section': <django.db.models.fields.CharField>,
但不知道如何纠正这个问题。