Django中的TabularInline类中的get_readonly_fields? [英] get_readonly_fields in a TabularInline class in Django?
问题描述
class ItemInline(admin.TabularInline):
model = Item
extra = 5
def get_readonly_fields(self,request,obj = None):
如果obj:
return ['name']
return self.readonly_fields
此代码是从另一个StackOverflow问题中获取的:
Django管理网站:防止字段被编辑? a>
然而,当它放在TabularInline类中时,新的对象表单不能正常呈现。目标是使某些字段只读,同时仍允许在新对象中输入数据。任何解决方法或不同策略的想法?
谨慎 - obj不是内联对象,它是父级。这可能是一个错误 - 例如这个Django票
I'm trying to use get_readonly_fields in a TabularInline class in Django:
class ItemInline(admin.TabularInline):
model = Item
extra = 5
def get_readonly_fields(self, request, obj=None):
if obj:
return ['name']
return self.readonly_fields
This code was taken from another StackOverflow question: Django admin site: prevent fields from being edited?
However, when it's put in a TabularInline class, the new object forms don't render properly. The goal is to make certain fields read only while still allowing data to be entered in new objects. Any ideas for a workaround or different strategy?
Careful - "obj" is not the inline object, it's the parent. That's arguably a bug - see for example this Django ticket
这篇关于Django中的TabularInline类中的get_readonly_fields?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!