[Fixed]-How to remove Add button in Django admin, for specific Model?

28👍

It is easy, just overload has_add_permission method in your Admin class like so:

class MyAdmin(admin.ModelAdmin):
    def has_add_permission(self, request):
        return False

8👍

To do it you need to override has_add_permission method in your Admin class:

class AmountOfBooksAdmin(admin.ModelAdmin):
    def has_add_permission(self, request):
        return False

    # to disable editing for specific or all fields
    # you can use readonly_fields attribute
    # (to see data you need to remove editable=False from fields in model):
    readonly_fields = ('book', 'amount')

Docs here: https://docs.djangoproject.com/en/dev/ref/contrib/admin/

👤ndpu

1👍

You asked about your design itself so I’ll address this first. If you want to keep an history of your stock then having a distinct table (model) makes sense but then you’d need a timestamp field and a unique constraint on (book, timestamp). Now if you just want to keep the stock current the relationship between book and amount is really one to one so the amount field really belongs to the book table. In this case you just have to add “amount” to the book’s ModelAdmin “readonly_fields” attribute and your problem is solved.

Leave a comment