Django管理员自定义验证 - 至少需要一个内联外键模型

时间:2021-07-24 07:15:24

I have two Django models (Purchaser and LineItem) that I manage via the stock admin interface. The dumbed-down versions:

我有两个Django模型(Purchaser和LineItem),我通过库存管理界面管理。愚蠢的版本:

class Purchaser(models.Model):
    firstname = models.CharField('First Name', max_length = 30)
    lastname = models.CharField('Last Name', max_length = 30)
    paymentid = models.IntegerField('Payment ID', unique = True)

class LineItem(models.Model):
    purchaser = models.ForeignKey(Purchaser)
    ship_first_name = models.CharField('Recipient First Name', max_length = 50)
    ship_last_name = models.CharField('Recipient Last Name', max_length = 50)

I have LineItems as an inline within the Purchaser admin page, and want to require that Purchasers have at least one LineItem (i.e. not let the user save a new Purchaser unless they have added at least one LineItem). Is there a clean way to do this? I already have some validation set up using a custom modelForm, but that method only deals with Purchaser fields, and not anything to do with LineItems. Advice?

我在买家管理页面中将LineItems作为内联,并希望要求购买者至少有一个LineItem(即,除非他们添加了至少一个LineItem,否则不要让用户保存新的购买者)。有干净的方法吗?我已经使用自定义modelForm设置了一些验证,但该方法仅处理Purchaser字段,而不涉及LineItems。建议吗?

1 个解决方案

#1


2  

You can use the answer information referenced here: Django: Forcing admin users to enter at least one item in TabularInline

您可以使用此处引用的答案信息:Django:强制管理员用户在TabularInline中输入至少一个项目

Hope that helps you out.

希望能帮到你。

#1


2  

You can use the answer information referenced here: Django: Forcing admin users to enter at least one item in TabularInline

您可以使用此处引用的答案信息:Django:强制管理员用户在TabularInline中输入至少一个项目

Hope that helps you out.

希望能帮到你。