0

我整晚都在寻找这个问题的答案,但似乎找不到问题所在。

我正在将自定义 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>}

我可以看到我添加的字段与模型中已经存在的字段的明显差异:'legacy_import': <django.forms.fields.TypedChoiceField object at 0x1c32c90>, 'section': <django.db.models.fields.CharField>, 但不知道如何纠正这个问题。

4

3 回答 3

2

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

self.fields['section_%d' % section.id] = forms.CharField(max_length=200, label=section.section_label)
于 2012-06-05T10:01:09.257 回答
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()})]
于 2012-06-05T15:27:23.150 回答
0

如果您没有在模型中定义主键,则可能会根据您的 django 环境设置自动创建名为 id 的列或其他内容作为主键。或者如果您直接在 db 中定义了一些列,但要做到这一点,您需要在 django 模型中编写它们的映射。要查看您的模型 sql,只需转到终端并键入:

# python manage.py sql [appname]

现在会有 django 为你生成的表 sql。所以你可以看到列。

于 2012-06-05T07:16:25.733 回答