Django disables editing (but allows adding) in the TabularInline view

I want to disable editing of ALL objects in a specific instance of TabularInline, while maintaining the ability to add and retaining the ability to edit the parent model.

I have a trivial setup:

class SuperviseeAdmin(admin.TabularInline): model = Supervisee class SupervisorAdmin(admin.ModelAdmin): inlines = [SuperviseeAdmin] admin.site.register(Supervisor, SupervisorAdmin) 

I tried to add the has_change_permission function to SuperviseeAdmin , which unconditionally returns False , but this did not affect.

I tried to set actions = None in SuperviseeAdmin , but this did not affect.

What can I ignore to make this work?

+9
source share
4 answers

Check out this solution: Django admin: make the field editable in the appendix, but not edit

Override the get_readonly_fields method:

 def get_readonly_fields(self, request, obj=None): if obj: # obj is not None, so this is an edit return ['name1',..] # Return a list or tuple of readonly fields' names else: # This is an addition return [] 
0
source

You can try creating a separate built-in class (see InlineModelAdmin docs ) that uses a custom ModelForm , where you can configure the clean method to create an error when trying to update:

 from django.contrib import admin from django.core.exceptions import ValidationError from django.forms import ModelForm from myapp.models import Supervisee class SuperviseeModelForm(ModelForm): class Meta(object): model = Supervisee # other options ... def clean(self): if self.instance.pk: # instance already exists raise ValidationError('Update not allowed') # instance doesn't exist yet, continue return super(SuperviseeModelForm, self).clean() class SuperviseeInline(admin.TabularInline): model = Supervisee form = SuperviseeModelForm class SuperviseeAdmin(admin.ModelAdmin): inlines = [SuperviseeInline] 
0
source

The user django admin embeds the has_change_permission () function and returns false to restrict viewing of the object.

 class SuperviseeAdmin(admin.TabularInline): model = Supervisee def has_change_permission(self, request): return False class SupervisorAdmin(admin.ModelAdmin): inlines = [SuperviseeAdmin] admin.site.register(Supervisor, SupervisorAdmin) 
0
source
 class SuperviseeAdmin(admin.TabularInline): model = Supervisee def __init__(self, *args, **kwargs): super(SuperviseeAdmin, self).__init__(*args, **kwargs) self.list_display_links = (None, ) 
-3
source

All Articles