B
B
blackbb2018-08-01 02:35:30
Django
blackbb, 2018-08-01 02:35:30

How to show fields of one model dynamically in Django admin?

Hello. There is a need to hide some fields in the admin panel of the page model. If this is the main page, then show all fields, if not the main page, then hide some fields. That is, when editing or adding pages through the admin panel, some fields were hidden on all pages except the main one. How to implement this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-08-01
@blackbb

class SomeModelAdmin(admins.ModelAdmin):
    fields = [...]  # Набор полей для всех страниц

    def get_fields(self, obj):
        fields = super().get_fields(obj)
        if obj.main:
            self.fields += [...]  # Поля только для главной страницы
        return fields

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question