Disable editing adding and deleting inline models in django admin on editing parent model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | class Inline(admin.TabularInline):
model = MyModel
extra = 0
class InlineReadOnly(Inline):
can_delete = False
def has_add_permission(self, request):
return False
def get_readonly_fields(self, request, obj=None):
result = list(set(
[field.name for field in self.opts.local_fields] +
[field.name for field in self.opts.local_many_to_many]
))
result.remove('id')
return result
class MyAdmin(admin.ModelAdmin):
def add_view(self, request, form_url='', extra_context=None):
self.inlines = [Inline, ]
return super(MyAdmin, self).add_view(request, form_url, extra_context)
def change_view(self, request, object_id, form_url='', extra_context=None):
self.inlines = [AttachmentInlineReadOnly, ]
return super(InlineReadOnly, self).change_view(request, object_id, form_url, extra_context)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.