Reputation: 171
I have been searching all night for an answer to this but can't seem to find what is wrong.
I am working on porting a custom CMS to Django, and basically it has the following structure:
So after the user creates and entry, when they edit it I want to show all the sections that are included in that layout.
So I am getting the layout ID from the entry, looping through and getting the sections, and trying to add them to the form.
When I debug it, it looks like everything is being added to the fields object just fine, but it doesn't show up on the form unless I predefine the fields, which would defeat the purpose
I have followed several examples here and have gotten to this 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)
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 = ""
An example of when I debug and print 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>}
I can see the obvious difference in the fields I added vs the fields that were already in
the model: 'legacy_import': <django.forms.fields.TypedChoiceField object at 0x1c32c90>, 'section': <django.db.models.fields.CharField>,
but not sure how to correct this.
Upvotes: 0
Views: 2354
Reputation: 171
Found the answer.
https://code.djangoproject.com/ticket/12238
Apparently there is a bug with adding fields to ModelForms.
Applying the change found in the comments of the link above to options.py seems to have fixed the issue and the fields now show up on the form.
I've solved this in the meantime by updating contrib/admin/options.py in:
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()})]
Upvotes: 1
Reputation: 599788
This is a form, not a model, so you want to add form fields, not model fields.
self.fields['section_%d' % section.id] = forms.CharField(max_length=200, label=section.section_label)
Upvotes: 2
Reputation: 2214
chances could be if you don't define primary key in your model then column named id or something according to you django environment setting will be created automatically as primary key. or it could be if you defined some columns in db directly but to do this you are required to write their mapping in django model. to see your model sql just go to terminal and type:
# python manage.py sql [appname]
Now there will be table sql generated by django for you. So you could see columns.
Upvotes: 0