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

发布于 2024-09-18 17:19:18 字数 437 浏览 6 评论 0原文

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

class GalleryForm(ModelForm):

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

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

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

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)

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少跟Wǒ拽 2024-09-25 17:19:18

在 lazerscience 和这篇帖子 我最终得到以下结果。

模型管理员:

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)

还有我的表格:

from django.contrib.admin.widgets import RelatedFieldWidgetWrapper


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) 

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

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:

from django.contrib.admin.widgets import RelatedFieldWidgetWrapper


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) 
心如狂蝶 2024-09-25 17:19:18

是的,你是对的,你必须用 django.contrib.admin.widgets.RelatedFieldWidgetWrapper 来包装你的小部件,这有点复杂,因为它需要当前管理站点作为初始化参数!也许您会发现这篇帖子对您有帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文