Login

django admin inlines readonly on change

Author:
mr-africa
Posted:
July 22, 2014
Language:
Python
Version:
1.6
Tags:
django admin inlines
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. Admin page without inlines dinamically by davmuz 2 years, 9 months ago
  2. Inline OneToOne relations in django-admin by bjourne 4 years, 10 months ago
  3. model save memos by depaolim 1 year, 9 months ago
  4. EditInline for GenericForeignKey by king 6 years, 10 months ago
  5. Making a django inline (model) formset really tabular by fnl 5 years, 11 months ago

Comments

Please login first before commenting.