Django 在 TabularInline 视图中禁用编辑(但允许添加)

发布于 2024-09-03 22:31:44 字数 517 浏览 5 评论 0原文

我想禁用编辑特定 TabularInline 实例中的所有对象,同时仍然允许添加并仍然允许编辑父模型。

我有一个简单的设置:

class SuperviseeAdmin(admin.TabularInline):
  model = Supervisee

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

admin.site.register(Supervisor, SupervisorAdmin)

我尝试向 SuperviseAdmin 添加一个 has_change_permission 函数,该函数无条件返回 False,但没有任何效果。

我尝试在 SuperviseAdmin 中设置 actions = None 但没有效果。

我可能会忽略什么才能让它发挥作用?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夏九 2024-09-10 22:31:44

用户 django admin 内置函数 has_change_permission() 并返回 false 以限制对象编辑视图。

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)

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)
百合的盛世恋 2024-09-10 22:31:44

您可以尝试创建一个单独的内联类(请参阅 InlineModelAdmin 文档),使用自定义 ModelForm,您可以在其中自定义 clean 方法以在尝试更新时引发错误:

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]

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]
迷途知返 2024-09-10 22:31:44

请参阅此解决方案: Django admin:在 add 中使字段可编辑但不编辑

重写 get_readonly_fields 方法:

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

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 []
浮世清欢 2024-09-10 22:31:44

只需将 admin.TabularInline 中的所有字段设置为 readonly_fields 即可:

class SuperviseeAdmin(admin.TabularInline):
  model = Supervisee

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

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

class SuperviseeAdmin(admin.TabularInline):
  model = Supervisee

  readonly_fields = ('your_field', 'your_field2',)
蓝海似她心 2024-09-10 22:31:44
class SuperviseeAdmin(admin.TabularInline):
  model = Supervisee

  def __init__(self, *args, **kwargs):
        super(SuperviseeAdmin, self).__init__(*args, **kwargs)
        self.list_display_links = (None, )
class SuperviseeAdmin(admin.TabularInline):
  model = Supervisee

  def __init__(self, *args, **kwargs):
        super(SuperviseeAdmin, self).__init__(*args, **kwargs)
        self.list_display_links = (None, )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文