2010-04-14 25 views
0

我正在py上編程和Django。我有型號: 從「子」表編輯ForeignKey

 
class Product(mymodels.Base): 
    title = models.CharField() 
    price = models.ForeignKey(Price) 
    promoPrice = models.ForeignKey(Price, related_name="promo_price")

class Price(mymodels.Base): value = models.DecimalField(max_digits=10, decimal_places=3) taxValue = models.DecimalField("Tax Value", max_digits=10, decimal_places=3) valueWithTax = models.DecimalField("Value with Tax", max_digits=10, decimal_places=3)

我想在編輯產品時看到兩種價格的INPUT,但找不到任何可能性。 inlines = [...]僅適用於從價格到產品,在這種情況下這很愚蠢。

感謝信。

+1

我想你需要使自己的表,告訴管理員使用它。 – diegueus9 2010-04-14 20:30:11

+0

我認爲你應該重新考慮你的模型。價格似乎不應該是它自己的模型。我會有價格和促銷價格數字字段,然後taxValue和valueWithTax是產品類的屬性 – Zach 2010-04-16 19:54:10

回答

0

如何使用此代替?

class Product(mymodels.Base): 
    title = models.CharField() 
    price = models.DecimalField(max_digits=10, decimal_places=3) 
    tax = models.DecimalField(max_digits=10, decimal_places=3) 
    promo_price = models.DecimalField(max_digits=10, decimal_places=3) 
    promo_tax = models.DecimalField(max_digits=10, decimal_places=3) 

    def price_with_tax(self): 
     return self.price + self.tax 

    def promo_price_with_tax(self):\ 
     return self.promo_price + self.promo_tax\ 

(附註:稅和promo_tax會是個不錯的候選人是ForeignKeys到將TaxRate型號)