Django 管理自定义验证 - 需要至少一个内联外键模型
我有两个 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)
我将 LineItems 作为购买者管理页面中的内联项,并希望要求购买者至少有一个 LineItem(即,不允许用户保存新的购买者,除非他们添加了至少一个 LineItem)。有没有一种干净的方法来做到这一点?我已经使用自定义 设置了一些验证modelForm,但该方法仅处理 Buyer 字段,与 LineItems 无关。建议?
I have two Django models (Purchaser and LineItem) that I manage via the stock admin interface. The dumbed-down versions:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用此处引用的答案信息: Django:强制管理员用户在 TabularInline 中输入至少一项
希望对您有所帮助。
You can use the answer information referenced here: Django: Forcing admin users to enter at least one item in TabularInline
Hope that helps you out.