How to show __str__ representations for existing Inline objects in the admin UI, but still use the form for adding new objects

huangapple go评论51阅读模式
英文:

How to show __str__ representations for existing Inline objects in the admin UI, but still use the form for adding new objects

问题

我有一个Django管理员视图,其中有许多嵌套视图。"添加另一个"按预期工作,并显示一个空表单以填入值。

但一旦添加,它会在表单视图中显示相关的对象,并且所有嵌套视图上的字段仍然可编辑。

我想实现的是,所有现有对象都以它们的__str__表示形式列出,但"添加另一个"按钮仍然会显示表单。

这在Django管理员中可行吗,还是我期望过高?

英文:

I have a Django Admin view with a number of inlines. The 'Add another <object>' works as expected and brings up an empty form to fill in the values.

Once added though, it shows that related object in form view, and all fields on all inlines remain editable.

What I'd like to achieve is that all existing objects are listed in their __str__ representation, but the 'Add another' button still brings up the form.

Can this be done in Django Admin, or am I looking for too much?

答案1

得分: 1

Django确实支持这个功能。这个功能是通过ModelAdmin.has_change_permission方法提供的。您可以像这样使用它:

class FoobarModelInline(admin.TabularInline):
    model = Foobar

    def has_change_permission(self, request, obj=None):
        return False

您可能还对has_delete_permission方法感兴趣。

英文:

Django does indeed support this. This functionality is provided by the ModelAdmin.has_change_permission method. You would use it like this:

class FoobarModelInline(admin.TabularInline):
    model = Foobar

    def has_change_permission(self, request, obj=None):
        return False

You might also be interested in the has_delete_permission method.

huangapple
  • 本文由 发表于 2023年4月19日 21:47:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定

  • 开发者交流平台

    本页二维码