N
N
NikClik2018-06-13 03:16:55
Django
NikClik, 2018-06-13 03:16:55

Display field depending on user status, how?

I have a model

class CompanyNews(models.Model):
...
    IsVisible = models.BooleanField(default=False)

I also let users with staff status view as admin.
The bottom line is that I need to make the field IsVisiblein the admin panel visible only to the superuser, how to do this?
PS It seems to me that this is done by overriding some method like get_querysetin admin.py, but I don't know, somehow there is not enough experience yet.
Thanks in advance for any info

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2018-06-13
@NikClik

You need to override the get_fields method :

@admin.register(CompanyNews)
class CompanyNewsAdmin(admin.ModelAdmin):
    ...

    def get_fields(self, request, obj=None):
        fields = super(StudentAdmin, self).get_fields(request, obj)
        if not request.user.is_superuser:
            fields.remove('IsVisible')
        return fields

E
Eugene, 2018-06-13
@immaculate

Django's built-in functionality certainly doesn't support this. You need to write your own view for the admin panel, in which you can check for the required permission.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question