Remove “add another” in Django admin screen

后端 未结 10 519
孤独总比滥情好
孤独总比滥情好 2020-12-05 13:34

Whenever I\'m editing object A with a foreign key to object B, a plus option \"add another\" is available next to the choices of object B. How do I remove that option?

相关标签:
10条回答
  • 2020-12-05 14:13

    django.contrib.admin.widgets.py

    (Django Install Dir)/django/contrib/admin/widgets.py: Comment everything between Line 239 & Line 244:

     if rel_to in self.admin_site._registry: # If the related object has an admin interface:
            # TODO: "id_" is hard-coded here. This should instead use the correct
            # API to determine the ID dynamically.
            output.append(u'<a href="%s" class="add-another" id="add_id_%s" onclick="return showAddAnotherPopup(this);"> ' % \
                (related_url, name))
            output.append(u'<img src="%simg/admin/icon_addlink.gif" width="10" height="10" alt="%s"/></a>' % (settings.ADMIN_MEDIA_PREFIX, _('Add Another')))
    
    0 讨论(0)
  • 2020-12-05 14:15

    Look at django.contrib.admin.options.py and check out the BaseModelAdmin class, formfield_for_dbfield method.

    You will see this:

    # For non-raw_id fields, wrap the widget with a wrapper that adds
    # extra HTML -- the "add other" interface -- to the end of the
    # rendered output. formfield can be None if it came from a
    # OneToOneField with parent_link=True or a M2M intermediary.
    if formfield and db_field.name not in self.raw_id_fields:
        formfield.widget = widgets.RelatedFieldWidgetWrapper(formfield.widget, db_field.rel, self.admin_site)
    

    I think your best bet is create subclass of ModelAdmin (which in turn is a subclass of BaseModelAdmin), base your model on that new class, override formfield_fo_dbfield and make it so that it won't/or will conditionally wrap the widget in RelatedFieldWidgetWrapper.

    One could argue that if you have a user that doesn't have rights to adding related objects, the RelatedFieldWidgetWrapper should not display the add link? Maybe this is something that is deserving of mention in Django trac?

    0 讨论(0)
  • 2020-12-05 14:17

    Though most of the solutions mentioned here work, there is another cleaner way of doing it. Probably it was introduced in a later version of Django, after the other solutions were presented. (I'm presently using Django 1.7)

    To remove the "Add another" option,

    class ... #(Your inline class)
    
        def has_add_permission(self, request):
            return False
    

    Similarly if you want to disable "Delete?" option, add the following method in Inline class.

        def has_delete_permission(self, request, obj=None):
            return False
    
    0 讨论(0)
  • 2020-12-05 14:19

    I'm using Django 2.x and I think I found best solution, at least for my case.

    The HTML file to the "Save and Add Another" button is on your_python_installation\Lib\site-packages\django\contrib\admin\templates\admin\subtmit_line.html.

    1. Copy that html file and paste to your project like so your_project\templates\admin\submit_line.html.
    2. Open it and comment/delete the button code as desired:

    {#{% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother" />{% endif %}#}

    I know this problem is already answered. But maybe someone in the future have a similar case with me.

    0 讨论(0)
提交回复
热议问题