简体   繁体   English

Django:根据其他字段更新字段值

[英]Django: Update Field Value Based on Other Fields

I am not sure this is even possible without modifying the Admin interface. 我不确定在不修改管理界面的情况下是否有可能。

I have a model called "Quote" that can contain multiple "Product" models. 我有一个名为“ Quote”的模型,该模型可以包含多个“ Product”模型。 I connect the two using an intermediate model "QuoteIncludes". 我使用中间模型“ QuoteIncludes”连接两者。 Here are the three models as they currently stand: 以下是目前的三种模型:

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

What I would like to be able to do is create a Quote in the Admin interface such that when I've filled in both the quantity and the per_unit_price of a line item, it fills in the "extended_price" as a product of the two when I tab over. 我想做的是在Admin界面中创建一个Quote,这样当我同时填写订单项的数量和per_unit_price时,它会在两者之间乘积“ extended_price”我签了。 I think it requires adding some AJAX in there. 我认为这需要在其中添加一些AJAX。

带注释的图片描述了我想要的

Info on how to include js in your model admin: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-media-definitions 有关如何在模型管理员中包含js的信息: http : //docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-media-definitions

For example: 例如:

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

And your script could look something like this: 您的脚本可能看起来像这样:

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));
        }
    });
});

Something like that. 这样的事情。

Just added the currency format function in case you wanted to use it. 只是添加了货币格式功能,以备不时之需。

You won't easily get that field in to the change list there because it belongs to another model from the one being editied. 您不会轻易将该字段输入到更改列表中,因为它属于正在编辑的另一种模型。 You wil be able to include the through models as an inline below this model, though, and then you could simply write some JS that takes your two input fields and generates the output value you want and plonks it into the appropriate field on the through model that's included in the inline. 不过,您可以将直通模型作为内联包含在此模型下,然后,您可以简单地编写一些JS,该JS接收您的两个输入字段并生成所需的输出值,并将其插入直通模型的相应字段中包含在内联中。

Or, write a custom view that doesn't lean on the admin ;o) 或者,编写不依赖于管理员的自定义视图; o)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM