Cannot hide “Save and add another” button in Django Admin

前端 未结 6 1392
余生分开走
余生分开走 2021-01-03 05:54

I would like to hide all the \"Save\" buttons in Django\'s Admin\'s Change Form, for a specific model, when certain conditions are met. Therefore, I override the chang

6条回答
  •  不知归路
    2021-01-03 06:55

    I have an alternative way to hide the "Save and add another" button.

    In form.py

    class TestForm(forms.ModelForm):
        class Media:
              js = ('admin/yourjsfile.js',)
    

    In yourjsfile.js

    (function($) {
    'use strict';
    $(document).ready(function() {
        // hide the "Save and add another" button
        $("input[name='_addanother']").attr('type','hidden');
    });
    })(django.jQuery);
    

    I had same issue for hiding that button. I've tested it in Django 1.11 and it did work, but I think there are better ways to achieve it.

提交回复
热议问题