这个问题可能看起来与这个问题相似,但它不是......
我有一个模型结构,如:
class Customer(models.Model):
....
class CustomerCompany(models.Model):
customer = models.ForeignKey(Customer)
type = models.SmallIntegerField(....)
我正在使用InlineModels
,并且有两种类型的CustomerCompany.type
. 所以我为CustomerCompany
和覆盖定义了两个不同的内联InlineModelAdmin.queryset
class CustomerAdmin(admin.ModelAdmin):
inlines=[CustomerCompanyType1Inline, CustomerCompanyType2Inline]
class CustomerCompanyType1Inline(admin.TabularInline):
model = CustomerCompany
def queryset(self, request):
return super(CustomerCompanyType1Inline, self).queryset(request).filter(type=1)
class CustomerCompanyType2Inline(admin.TabularInline):
model = CustomerCompany
def queryset(self, request):
return super(CustomerCompanyType2Inline, self).queryset(request).filter(type=2)
到目前为止一切都很好,但是为了添加新记录InlineModelAdmin
,我仍然需要在 上显示字段type
,因为我无法覆盖类似的方法 :CustomerCompany
AdminForm
save
InlineModelAdmin
class CustomerCompanyType2Inline(admin.TabularInline):
model = CustomerCompany
def queryset(self, request):
return super(CustomerCompanyType2Inline, self).queryset(request).filter(type=2)
#Following override do not work
def save_model(self, request, obj, form, change):
obj.type=2
obj.save()
使用信号也不是解决方案,因为我的信号sender
将是相同的Model
,所以我无法检测到哪个InlineModelAdmin
发送它以及type
必须是什么......
有没有办法让我type
在保存之前设置字段?