2010-08-25 123 views
0

我不確定這甚至可以在不修改管理界面的情況下實現。Django:基於其他字段更新字段值

我有一個名爲「Quote」的模型,可以包含多個「產品」模型。我使用中間模型「QuoteIncludes」連接兩個。這裏有三種模式,因爲他們目前站:

class Product(models.Model): 
    name = models.CharField(max_length=100) 
    short_desc = models.CharField(max_length=200) 
    default_cost = models.DecimalField(max_digits=15, decimal_places=2) 
    default_price = models.DecimalField(max_digits=15, decimal_places=2) 
    shipping_per_unit = models.DecimalField(max_digits=9, decimal_places=2) 
    weight_in_lbs = models.DecimalField(max_digits=5, decimal_places=2) 

    def __unicode__(self): 
     return self.name 

class Quote(models.Model): 

    ## Human name for easy reference 
    name = models.CharField(max_length=100) 
    items = models.ManyToManyField(Product, through='QuoteIncludes') 

    def __unicode__(self): 
     return self.name 

class QuoteIncludes(models.Model): 

    ## Attach foreign keys between a Quote and Product 
    product = models.ForeignKey(Product) 
    quote = models.ForeignKey(Quote) 

    ## Additional fields when adding product to a Quote 
    quantity = models.PositiveIntegerField() 
    per_unit_cost = models.DecimalField(max_digits=15, decimal_places=2) 
    per_unit_price = models.DecimalField(max_digits=15, decimal_places=2) 

    def _get_extended_price(self): 
     """Gets extended price by multiplying quantity and unit price.""" 
     if self.quantity and self.per_unit_price: 
      return self.quantity * self.per_unit_price 
     else: 
      return 0.00 

    extended_price = _get_extended_price 

我想做什麼就能做的是建立在管理界面中的報價,這樣當我填寫的數量和a的per_unit_price訂單項,當我標籤結束時,它會填充「extended_price」作爲兩者的產品。我認爲它需要在那裏添加一些AJAX。

Annotated picture describing what I would like

+0

無論您使用哪種解決方案,如果您不希望用戶能夠以自己的價格提交任意值,請注意安全性。 – user27478 2010-08-26 20:10:47

+0

那麼,這個觀點將用於「引用」產品列表,因此編輯這些編號的人員能夠根據需要調整它們是有意的。感謝您指出它。 – akoumjian 2010-08-27 13:37:32

回答

3

信息如何在您的模型管理員JS: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-media-definitions

例如:

class Media: 
    js = (
     'http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js', 
     '/media/js/calculate.js', 
    ) 

而你的腳本可能看起來像這樣:

function currencyFormat(nStr) { 
    nStr += ''; 
    x = nStr.split('.'); 
    x1 = x[0]; 
    x2 = x.length > 1 ? '.' + x[1] : ''; 
    var rgx = /(\d+)(\d{3})/; 
    while (rgx.test(x1)) { 
     x1 = x1.replace(rgx, '$1' + '.' + '$2'); 
    } 
    return x1 + x2; 
} 

jQuery(document).ready(function($){ 
    $('input[id$=quantity], input[id$=per_unit_cost]').live('keyup', function() { 
     var $tr = $(this).parents('tr'); 
     var quantity = parseInt($tr.find('input[id$=quantity]').val()); 
     var count = parseInt($tr.find('input[id$=per_unit_cost]').val()); 

     if(quantity && count) { 
      $tr.find('input[id$=per_unit_price]').html(currencyFormat(quantity * count)); 
     } 
    }); 
}); 

就是這樣的。

剛剛添加了貨幣格式功能,以防您想要使用它。

+0

謝謝,無論是資源和功能的例子。 – akoumjian 2010-08-27 13:35:12

0

你不會輕易讓該字段中更改列表那裏,因爲它屬於另一個模型從一個被editied。儘管如此,您仍然可以將直通模型作爲內聯模型包含在內,然後您可以簡單地編寫一些JS,該JS使用您的兩個輸入字段並生成所需的輸出值,並將其插入到貫穿模型的相應字段中這包含在內聯中。

或者,寫不上管理員瘦了自定義視圖; O)

+0

我不關心更改列表中的該字段,因爲它在代碼中顯示。它可以作爲基本的十進制字段存在。 我真正在尋找的是與JS的一些方向,並使用該管理視圖或開發新視圖。 – akoumjian 2010-08-26 15:57:58

相關問題