M
M
maxclax2015-03-31 02:25:33
Django
maxclax, 2015-03-31 02:25:33

How to tweak the admin?

I have some report of logs of actions of the user. And in fact, I don’t need the ability to add a new log through the admin panel and edit existing ones. How to implement it? That is, remove the ability to add and update the model through the admin panel?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anatoly Scherbakov, 2015-03-31
@maxclax

Can you see these models?

class MyAdmin(admin.ModelAdmin):
    fields = ['name', 'slug', 'date']
    readonly_fields = ['date']

    def has_add_permission(self):
        # Убрать право добавлять новые элементы
        return False

     def get_readonly_fields(self, request, obj=None):
          if obj and obj.pk:
               return self.fields
          else:
               return self.readonly_fields

If the object is saved, then all fields specified in fields will be declared read-only. Otherwise, only those specified in readonly_fields . It won't work if you don't have fields defined , but fieldsets - then set the fields manually or consider the obj._meta.fields object (if I'm not mistaken).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question