简体   繁体   English

Django将字段添加到模型-字段未显示在表单上

[英]Django Adding a field to a model - field not showing on form

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: 我正在将自定义CMS移植到Django,基本上它具有以下结构:

  • An Entry has 1 layout 条目具有1个布局
  • A layout has many sections 布局有很多部分

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. 所以我要从条目中获取布局ID,遍历并获取各节,然后尝试将它们添加到表单中。

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 当我调试它时,看起来一切都很好地添加到了fields对象中,但是除非我预先定义了fields,否则它不会显示在表单上,​​否则将无法达到目的。

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 我调试和打印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. 我可以看到添加的字段与模型中已添加的字段之间的明显差异: 'legacy_import': <django.forms.fields.TypedChoiceField object at 0x1c32c90>, 'section': <django.db.models.fields.CharField>,但不确定如何更正此问题。

这是一个表单,而不是模型,因此您要添加表单字段,而不是模型字段。

self.fields['section_%d' % section.id] = forms.CharField(max_length=200, label=section.section_label)

Found the answer. 找到了答案。

https://code.djangoproject.com/ticket/12238 https://code.djangoproject.com/ticket/12238

Apparently there is a bug with adding fields to ModelForms. 显然有一个向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. 将在上面链接的注释中找到的更改应用于options.py似乎已解决了该问题,并且字段现在显示在表单上。

I've solved this in the meantime by updating contrib/admin/options.py in: 在此期间,我已经通过更新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()})] 

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. 如果您没有在模型中定义主键,则可能会自动创建名为id或根据您的django环境设置名称的列作为主键。 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. 或者可能是如果您直接在db中定义了一些列,但要这样做,则需要在django模型中编写它们的映射。 to see your model sql just go to terminal and type: 要查看您的模型sql,只需转到终端并输入:

# python manage.py sql [appname]

Now there will be table sql generated by django for you. 现在将有django为您生成的表sql。 So you could see columns. 这样您就可以看到列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM