1. <i id='TiB5D'><tr id='TiB5D'><dt id='TiB5D'><q id='TiB5D'><span id='TiB5D'><b id='TiB5D'><form id='TiB5D'><ins id='TiB5D'></ins><ul id='TiB5D'></ul><sub id='TiB5D'></sub></form><legend id='TiB5D'></legend><bdo id='TiB5D'><pre id='TiB5D'><center id='TiB5D'></center></pre></bdo></b><th id='TiB5D'></th></span></q></dt></tr></i><div id='TiB5D'><tfoot id='TiB5D'></tfoot><dl id='TiB5D'><fieldset id='TiB5D'></fieldset></dl></div>
  2. <tfoot id='TiB5D'></tfoot>
    • <bdo id='TiB5D'></bdo><ul id='TiB5D'></ul>

    1. <legend id='TiB5D'><style id='TiB5D'><dir id='TiB5D'><q id='TiB5D'></q></dir></style></legend>
    2. <small id='TiB5D'></small><noframes id='TiB5D'>

      Django admin - 如何在自定义管理表单中为多对多字段添加绿色加号

      Django admin - How can I add the green plus sign for Many-to-many Field in custom admin form(Django admin - 如何在自定义管理表单中为多对多字段添加绿色加号)
      <tfoot id='Cl4hL'></tfoot>

          <tbody id='Cl4hL'></tbody>

          <i id='Cl4hL'><tr id='Cl4hL'><dt id='Cl4hL'><q id='Cl4hL'><span id='Cl4hL'><b id='Cl4hL'><form id='Cl4hL'><ins id='Cl4hL'></ins><ul id='Cl4hL'></ul><sub id='Cl4hL'></sub></form><legend id='Cl4hL'></legend><bdo id='Cl4hL'><pre id='Cl4hL'><center id='Cl4hL'></center></pre></bdo></b><th id='Cl4hL'></th></span></q></dt></tr></i><div id='Cl4hL'><tfoot id='Cl4hL'></tfoot><dl id='Cl4hL'><fieldset id='Cl4hL'></fieldset></dl></div>
        1. <small id='Cl4hL'></small><noframes id='Cl4hL'>

            <legend id='Cl4hL'><style id='Cl4hL'><dir id='Cl4hL'><q id='Cl4hL'></q></dir></style></legend>

                <bdo id='Cl4hL'></bdo><ul id='Cl4hL'></ul>

                本文介绍了Django admin - 如何在自定义管理表单中为多对多字段添加绿色加号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                当我在表单中定义多选字段(照片)时,用于在管理表单中添加新实例的绿色加号按钮消失了.即,删除带有定义的行 (photos = ...) 会使加号出现.但是,为了使用自定义字段/小部件,我需要弄清楚这一点.

                The green plus sign button for adding new instances in the admin form disappears for my MultiSelect field (photos) when I define it in my form. Ie, removing the line with the definition (photos = ...) makes the plus sign appear. However, in order to use a custom Field/Widget I need to figure this out.

                class GalleryForm(ModelForm):
                
                    photos = ModelMultipleChoiceField(queryset=Photo.objects.all(), label="Photos")
                
                    def __init__(self, *args, **kwargs):
                        super(GalleryForm, self).__init__(*args, **kwargs)
                

                我查看了 Django 源代码,似乎我必须将我的小部件包装在 RelatedFieldWidgetWrapper 中,但我还没有完全理解它.感谢任何帮助!

                I've peeked at the Django source code and it seems like I have to wrap my widget in a RelatedFieldWidgetWrapper, but I haven't quite gotten my head around it. Any help is apprecietad!

                推荐答案

                借助 lazerscience 和这个 post 我得到了以下结果.

                With the help from lazerscience and this post I ended up with the following.

                模型管理员:

                class GalleryAdmin(admin.ModelAdmin):
                
                    form = GalleryForm
                
                    def __init__(self, model, admin_site):
                        self.form.admin_site = admin_site 
                        super(GalleryAdmin, self).__init__(model, admin_site)
                

                还有我的表格:

                class GalleryForm(ModelForm):
                
                    photos = ThumbnailChoiceField(queryset=Photo.objects.all(), label='Photos', widget=MyWidget(), required=False)
                
                    def __init__(self, *args, **kwargs):
                        super(GalleryForm, self).__init__(*args, **kwargs)
                        rel = ManyToOneRel(self.instance.photos.model, 'id') 
                        self.fields['photos'].widget = RelatedFieldWidgetWrapper(self.fields['photos'].widget, rel, self.admin_site) 
                

                这篇关于Django admin - 如何在自定义管理表单中为多对多字段添加绿色加号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
              • <small id='r87kd'></small><noframes id='r87kd'>

                  <tbody id='r87kd'></tbody>
              • <i id='r87kd'><tr id='r87kd'><dt id='r87kd'><q id='r87kd'><span id='r87kd'><b id='r87kd'><form id='r87kd'><ins id='r87kd'></ins><ul id='r87kd'></ul><sub id='r87kd'></sub></form><legend id='r87kd'></legend><bdo id='r87kd'><pre id='r87kd'><center id='r87kd'></center></pre></bdo></b><th id='r87kd'></th></span></q></dt></tr></i><div id='r87kd'><tfoot id='r87kd'></tfoot><dl id='r87kd'><fieldset id='r87kd'></fieldset></dl></div>
                  • <legend id='r87kd'><style id='r87kd'><dir id='r87kd'><q id='r87kd'></q></dir></style></legend>
                        <bdo id='r87kd'></bdo><ul id='r87kd'></ul>

                        1. <tfoot id='r87kd'></tfoot>