我一直在尋找整夜尋找答案,但似乎無法找到問題所在。Django添加一個字段到模型 - 字段不顯示在窗體上
我的工作移植自定義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>}
我可以看到obvi我添加的字段與已經在 模型中的字段有所不同:'legacy_import': <django.forms.fields.TypedChoiceField object at 0x1c32c90>, 'section': <django.db.models.fields.CharField>,
但不確定如何解決此問題。
迪迪ü手動添加的領域在數據庫中....... ..? – user1409289
該字段位於數據庫中,但不在該模型中。這是另一種模式。 – Jimmy
我將不得不編寫自定義保存功能,但需要在表格 – Jimmy