英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论