Django 1.7 removing Add button from inline form

后端 未结 4 1700

I\'m having problems achieving a (probably) rather simple task. I have fully modifiable models (Prodotto, Comune) which are shown as \"addable\" fields, as shown in picture

4条回答
  •  心在旅途
    2020-12-16 01:16

    I think this is a less hacky solution than the one you ended up with. It worked for me, anyway.

    Basically, it's the inline equivalent of what you suggested doing with the overriding the get_form method of ModelAdmin. Here we override get_formset in the inline class, get the form off the formset, and do the exact same thing. Seems to work fine, at least in 1.9, which I am using.

    class VersionEntryInline(admin.TabularInline):
        template = 'admin/edit_inline/tabular_versionentry.html'
        model = VersionEntry
        extra = 0
    
        def get_formset(self, request, obj=None, **kwargs):
            """
            Override the formset function in order to remove the add and change buttons beside the foreign key pull-down
            menus in the inline.
            """
            formset = super(VersionEntryInline, self).get_formset(request, obj, **kwargs)
            form = formset.form
            widget = form.base_fields['project'].widget
            widget.can_add_related = False
            widget.can_change_related = False
            widget = form.base_fields['version'].widget
            widget.can_add_related = False
            widget.can_change_related = False
            return formset
    

提交回复
热议问题