2011-05-01 42 views
5

我有兩個我通過股票管理界面管理的Django模型(買方和LineItem)。該簡單化向下版本:Django admin自定義驗證 - 至少需要一個內聯外鍵模型

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) 

我了LineItem作爲買方的管理頁面中內嵌了,想要求買家至少有一個LineItem的(即不是讓用戶保存一個新的購買者,除非他們增加至少一個LineItem)。有沒有一種乾淨的方式來做到這一點?我已經使用自定義modelForm設置了一些驗證,但該方法僅處理Purchase字段,而不處理與LineItem相關的任何操作。建議嗎?

回答

相關問題