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

时间:2022-10-04 14:15:34

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.

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

class GalleryForm(ModelForm):

    photos = ModelMultipleChoiceField(queryset=Photo.objects.all(), label="Photos")

    def __init__(self, *args, **kwargs):
        super(GalleryForm, self).__init__(*args, **kwargs)

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!

我偷看了Django的源代码,似乎我必须将我的小部件包装在一个RelatedFieldWidgetWrapper中,但我还没有完全理解它。任何帮助都是赞赏的!

2 个解决方案

#1


10  

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

在lazerscience和这篇文章的帮助下,我最终获得了以下内容。

The ModelAdmin:

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)

And my form:

我的表格:

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) 

#2


8  

Yes you are right, you have to wrap your widget with django.contrib.admin.widgets.RelatedFieldWidgetWrapper, which turns out to be a bit complicated since it expects the current admin site as a parameter for initialization! Maybe you will find this post helpful!

是的,你是对的,你必须用django.contrib.admin.widgets.RelatedFieldWidgetWrapper包装你的小部件,结果有点复杂,因为它期望当前的管理站点作为初始化的参数!也许你会发现这篇文章很有帮助!

#1


10  

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

在lazerscience和这篇文章的帮助下,我最终获得了以下内容。

The ModelAdmin:

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)

And my form:

我的表格:

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) 

#2


8  

Yes you are right, you have to wrap your widget with django.contrib.admin.widgets.RelatedFieldWidgetWrapper, which turns out to be a bit complicated since it expects the current admin site as a parameter for initialization! Maybe you will find this post helpful!

是的,你是对的,你必须用django.contrib.admin.widgets.RelatedFieldWidgetWrapper包装你的小部件,结果有点复杂,因为它期望当前的管理站点作为初始化的参数!也许你会发现这篇文章很有帮助!