Login

django admin inlines readonly on change

Author:
mr-africa
Posted:
July 22, 2014
Language:
Python
Version:
1.6
Score:
0 (after 0 ratings)

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

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 3 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.