我一整夜都在寻找答案,但似乎无法找到问题所在。
我正在努力将自定义CMS移植到Django,基本上它具有以下结构:
因此,在用户创建和输入后,当他们编辑它时,我想显示该布局中包含的所有部分。
所以我从条目中获取布局ID,循环并获取这些部分,并尝试将它们添加到表单中。
当我调试它时,看起来所有东西都被添加到fields对象中就好了,但它没有显示在表单上,除非我预定义字段,这会破坏目的
我已经在这里看了几个例子并且已经得到了这段代码:
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>,
但不知道如何纠正这一点。
答案 0 :(得分:2)
这是一个表单,而不是模型,因此您要添加表单字段,而不是模型字段。
self.fields['section_%d' % section.id] = forms.CharField(max_length=200, label=section.section_label)
答案 1 :(得分:1)
找到答案。
https://code.djangoproject.com/ticket/12238
显然,向ModelForms添加字段存在错误。
将上述链接的评论中找到的更改应用于options.py似乎修复了问题,现在字段显示在表单上。
我在此期间通过更新contrib / admin / options.py解决了这个问题 在:
def get_fieldsets(self, request, obj=None): "Hook for specifying fieldsets for the add form." if self.declared_fieldsets: return self.declared_fieldsets #form = self.get_form(request, obj) #return [(None, {'fields': form.base_fields.keys()})] form = self.get_form(request, obj)(instance=obj) return [(None, {'fields': form.fields.keys()})]
答案 2 :(得分:0)
如果你没有在模型中定义主键,那么可能是你的django环境设置将根据你的django环境设置自动创建主键。或者如果你直接在db中定义了一些列,但为了做到这一点,你需要在django模型中编写它们的映射。要查看你的模型sql,只需转到终端并输入:
# python manage.py sql [appname]
现在有django为你生成的表sql。所以你可以看到专栏。