VoteyDisciple
VoteyDisciple

Reputation: 37813

Django disable editing (but allow adding) in TabularInline view

I want to disable editing ALL objects within a particular TabularInline instance, while still allowing additions and while still allowing editing of the parent model.

I have this trivial setup:

class SuperviseeAdmin(admin.TabularInline):
  model = Supervisee

class SupervisorAdmin(admin.ModelAdmin):
  inlines = [SuperviseeAdmin]

admin.site.register(Supervisor, SupervisorAdmin)

I have tried adding a has_change_permission function to SuperviseeAdmin that returns False unconditionally, but it had no effect.

I have tried setting actions = None in SuperviseeAdmin but it had no effect.

What might I be overlooking that could get this to work?

Upvotes: 12

Views: 6694

Answers (5)

agence web JHN
agence web JHN

Reputation: 49

just make all your fields readonly_fields in your admin.TabularInline as :

class SuperviseeAdmin(admin.TabularInline):
  model = Supervisee

  readonly_fields = ('your_field', 'your_field2',)

Upvotes: -1

SuReSh
SuReSh

Reputation: 1511

User django admin build in function has_change_permission() and return false to restrict object Edit view.

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)

Upvotes: 4

Villiers Strauss
Villiers Strauss

Reputation: 395

You can try creating a separate inline class (see the InlineModelAdmin docs) that uses a custom ModelForm where you can customise the the clean method to throw 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]

Upvotes: 1

Sudipta
Sudipta

Reputation: 4971

See this solution: Django admin: make field editable in add but not edit

Override 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 []

Upvotes: 0

russjman
russjman

Reputation: 506

class SuperviseeAdmin(admin.TabularInline):
  model = Supervisee

  def __init__(self, *args, **kwargs):
        super(SuperviseeAdmin, self).__init__(*args, **kwargs)
        self.list_display_links = (None, )

Upvotes: -4

Related Questions