python - Django Adding a field to a model - field not showing on form -
i have been searching night answer can't seem find wrong.
i working on porting custom cms django, , has following structure:
- an entry has 1 layout
- a layout has many sections
so after user creates , entry, when edit want show sections included in layout.
so getting layout id entry, looping through , getting sections, , trying add them form.
when debug it, looks being added fields object fine, doesn't show on form unless predefine fields, defeat purpose
i have followed several examples here , have gotten code:
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) 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 = "" an example of when debug , print self.fields
{'layout': <django.forms.models.modelchoicefield object @ 0x1c30b50>, 'uid': <django.forms.fields.integerfield object @ 0x1c30c50>, 'url': <django.forms.fields.charfield object @ 0x1c30cd0>, 'url_301': <django.forms.fields.charfield object @ 0x1c30d50>, 'name': <django.forms.fields.charfield object @ 0x1c30dd0>, 'page_title': <django.forms.fields.charfield object @ 0x1c30e50>, 'meta_description': <django.forms.fields.charfield object @ 0x1c30ed0>, 'meta_keywords': <django.forms.fields.charfield object @ 0x1c30f50>, 'order_fld': <django.forms.fields.charfield object @ 0x1c30fd0>, 'user_id': <django.forms.fields.integerfield object @ 0x1c32090>, 'author': <django.forms.models.modelchoicefield object @ 0x1c32110>, 'date_active': <django.forms.fields.splitdatetimefield object @ 0x1c32210>, 'date_added': <django.forms.fields.splitdatetimefield object @ 0x1c32290>, 'date_modified': <django.forms.fields.splitdatetimefield object @ 0x1c32390>, 'date_expires': <django.forms.fields.splitdatetimefield object @ 0x1c32490>, 'date_published': <django.forms.fields.splitdatetimefield object @ 0x1c32590>, 'is_active': <django.forms.fields.typedchoicefield object @ 0x1c32690>, 'hide_from_dropdown': <django.forms.fields.typedchoicefield object @ 0x1c32790>, 'is_featured': <django.forms.fields.typedchoicefield object @ 0x1c32810>, 'in_sitemap': <django.forms.fields.typedchoicefield object @ 0x1c32890>, 'admin_user_id': <django.forms.fields.integerfield object @ 0x1c32910>, 'show_large_photo': <django.forms.fields.typedchoicefield object @ 0x1c32990>, 'is_featured_on_homepage': <django.forms.fields.typedchoicefield object @ 0x1c32a10>, 'capitalize_first_letter': <django.forms.fields.typedchoicefield object @ 0x1c32a90>, 'display_share_box': <django.forms.fields.typedchoicefield object @ 0x1c32b10>, 'display_subscribe_box': <django.forms.fields.typedchoicefield object @ 0x1c32b90>, 'is_commenting_enabled': <django.forms.fields.typedchoicefield object @ 0x1c32c10>, 'legacy_import': <django.forms.fields.typedchoicefield object @ 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 @ 0x1c30b50>, 'uid': <django.forms.fields.integerfield object @ 0x1c30c50>, 'url': <django.forms.fields.charfield object @ 0x1c30cd0>, 'url_301': <django.forms.fields.charfield object @ 0x1c30d50>, 'name': <django.forms.fields.charfield object @ 0x1c30dd0>, 'page_title': <django.forms.fields.charfield object @ 0x1c30e50>, 'meta_description': <django.forms.fields.charfield object @ 0x1c30ed0>, 'meta_keywords': <django.forms.fields.charfield object @ 0x1c30f50>, 'order_fld': <django.forms.fields.charfield object @ 0x1c30fd0>, 'user_id': <django.forms.fields.integerfield object @ 0x1c32090>, 'author': <django.forms.models.modelchoicefield object @ 0x1c32110>, 'date_active': <django.forms.fields.splitdatetimefield object @ 0x1c32210>, 'date_added': <django.forms.fields.splitdatetimefield object @ 0x1c32290>, 'date_modified': <django.forms.fields.splitdatetimefield object @ 0x1c32390>, 'date_expires': <django.forms.fields.splitdatetimefield object @ 0x1c32490>, 'date_published': <django.forms.fields.splitdatetimefield object @ 0x1c32590>, 'is_active': <django.forms.fields.typedchoicefield object @ 0x1c32690>, 'hide_from_dropdown': <django.forms.fields.typedchoicefield object @ 0x1c32790>, 'is_featured': <django.forms.fields.typedchoicefield object @ 0x1c32810>, 'in_sitemap': <django.forms.fields.typedchoicefield object @ 0x1c32890>, 'admin_user_id': <django.forms.fields.integerfield object @ 0x1c32910>, 'show_large_photo': <django.forms.fields.typedchoicefield object @ 0x1c32990>, 'is_featured_on_homepage': <django.forms.fields.typedchoicefield object @ 0x1c32a10>, 'capitalize_first_letter': <django.forms.fields.typedchoicefield object @ 0x1c32a90>, 'display_share_box': <django.forms.fields.typedchoicefield object @ 0x1c32b10>, 'display_subscribe_box': <django.forms.fields.typedchoicefield object @ 0x1c32b90>, 'is_commenting_enabled': <django.forms.fields.typedchoicefield object @ 0x1c32c10>, 'legacy_import': <django.forms.fields.typedchoicefield object @ 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>} i can see obvious difference in fields added vs fields in model: 'legacy_import': <django.forms.fields.typedchoicefield object @ 0x1c32c90>, 'section': <django.db.models.fields.charfield>, not sure how correct this.
this form, not model, want add form fields, not model fields.
self.fields['section_%d' % section.id] = forms.charfield(max_length=200, label=section.section_label)
Comments
Post a Comment