我有一个与另一个模型相关的模型。
class Vehicle(models.Model):
number = models.CharField(max_length=14)
class Seat(models.Model):
matierial = models.CharField(max_length=10)
vehicle = models.OneToOneField(Vehicle, on_delete=models.PROTECT)
def clean(self):
"""
This method is called before saving via admin panel
"""
if self.matierial == 'jute':
raise ValidationError(_('Jute products are not allowed through admin panel'))
现在,我正在这样在管理面板中使用内联模型
class SeatInlineAdmin(admin.StackedInline):
model = Seat
extra = 0
class VehicleAdmin(admin.ModelAdmin):
inlines = [SeatInlineAdmin]
但是我有一个问题,
如果车辆v1
与座位s1
有关,而座位s1
是jute
的材料,并且我想更改车辆v1
的编号从管理面板上,它抛出了错误消息
'Jute products are not allowed through admin panel'
因此,这意味着当我尝试保存车辆时,它正在尝试通过InlineAdmin保存Seat模型。
从InlineAdmin调用时,有什么方法可以绕过clean()
方法吗?