Django admin - make all fields readonly

后端 未结 9 1169
春和景丽
春和景丽 2020-12-05 17:56

I\'m trying to make all fields readonly without listing them explicitly.

Something like:

class CustomAdmin(admin.ModelAdmin):
    def get_readonly_fi         


        
相关标签:
9条回答
  • 2020-12-05 18:00

    Careful, self.model._meta.fields are not necessarily the same fields that CustomAdmin has!

    "All fields of the Admin" would look more like this:

    from django.contrib import admin
    from django.contrib.admin.utils import flatten_fieldsets
    
    class CustomAdmin(admin.ModelAdmin):
        def get_readonly_fields(self, request, obj=None):
            if request.user.is_superuser:
                return self.readonly_fields
    
            if self.declared_fieldsets:
                return flatten_fieldsets(self.declared_fieldsets)
            else:
                return list(set(
                    [field.name for field in self.opts.local_fields] +
                    [field.name for field in self.opts.local_many_to_many]
                ))
    
    0 讨论(0)
  • 2020-12-05 18:06

    Ok, now there's this:

    class CustomAdmin(admin.ModelAdmin):
        def get_readonly_fields(self, request, obj=None):
            # ...
    
            return [f.name for f in self.model._meta.fields]
    

    Still looking for a less ugly way.

    0 讨论(0)
  • 2020-12-05 18:11

    You could iterate through the model meta fields:

    def get_readonly_fields(self, request, obj=None):
        if obj:
            self.readonly_fields = [field.name for field in obj.__class__._meta.fields]
        return self.readonly_fields
    
    0 讨论(0)
  • 2020-12-05 18:13

    My requirement was similar . I needed only one field to be shown as read-only . And this worked fine:

    class ChoiceInline(admin.TabularInline):
        model = Choice
        extra = 1
        fields = ['choice_text', 'votes']
        readonly_fields = ['votes']
    
    class QuestionAdmin(admin.ModelAdmin):
        #fields = ['pub_date', 'question_text']
        fieldsets = [
            (None, {'fields': ['question_text']}),
            ('Date Information', {'fields': ['pub_date']}),
        ]
        search_fields = ['question_text']
    
    
        inlines = [ChoiceInline]
    

    Refer: C:\Python27\Lib\site-packages\django\contrib\admin\options.py

    0 讨论(0)
  • 2020-12-05 18:18

    For Inlines (Tab or Stack)

    def get_readonly_fields(self, request, obj=None):
        fields = []
        for field in self.model._meta.get_all_field_names():
            if field != 'id':
                fields.append(field)
        return fields
    
    def has_add_permission(self, request):
        return False
    
    0 讨论(0)
  • 2020-12-05 18:22

    Since django 2.1, you can prevent editing, while allowing viewing, by returning False from the ModelAdmin's has_change_permission method, like this:

    class CustomAdmin(admin.ModelAdmin):
        def has_change_permission(self, request, obj=None):
            return False
    

    (This will not work before django 2.1, as it will also deny permission to any user trying only to view.)

    0 讨论(0)
提交回复
热议问题