您好我正在使用动态内联创建模型管理员。
class InlineProfile(admin.TabularInline):
model = Profile
# max_num = 1
class InlineProfileOne(admin.TabularInline):
model = TableOne
# max_num = 1
class InlineProfileTwo(admin.TabularInline):
model = TableTwo
# max_num = 1
class UserDisplay(admin.ModelAdmin):
inlines = [InlineProfile]
def get_inline_instances(self, request, obj=None):
up=UserProfile.objects.get(pk=obj.id)
if (up.job_type.job_type==1):
self.inlines.append(InlineProfileOne)
elif (up.job_type.job_type==2):
self.inlines.append(InlineProfileTwo)
return obj and super(UserDisplay, self).get_inline_instances(request, obj) or []
# def get_formsets(self, request, obj):
# print obj
# for inline in self.get_inline_instances(request, obj):
# yield inline.get_formset(request, obj)
list_display = [f.name for f in LCUser._meta.fields]
field_set = list(list_display)
field_set.remove('id')
field_set.remove('api_key')
field_set.remove('api_secret')
readonly_fields=['last_login_ip','last_login_timestamp','created_at','updated_at']
fieldsets = (
(None, {
'fields': field_set,
}),
('Advanced options', {
'classes': ('collapse',),
'fields': ('api_key', 'api_secret'),
}),
)
search_fields=list_display
list_filter=['user_type','profile_complete']
list_editable=list(list_display)
list_editable.remove('id')
list_editable.remove('updated_at')
list_editable.remove('created_at')
list_editable.remove('last_login_timestamp')
list_editable.remove('last_login_ip')
class Meta:
model = LCUser
显示我有时看到两个内联行而不是一行,当我尝试更新它时,我得到“ManagementForm数据丢失或被篡改”
答案 0 :(得分:0)
弄清楚我做错了什么。看起来我忘了初始化inlinse变量。所以每当我点击一个对象时,它再次添加了相同的内联模型,这在我尝试保存时会导致错误。