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

发布于 2024-09-15 16:34:30 字数 1717 浏览 5 评论 0原文

我不确定在不修改管理界面的情况下这是否可能。

我有一个名为“报价”的模型,它可以包含多个“产品”模型。我使用中间模型“QuoteInincludes”将两者连接起来。以下是目前的三个模型:

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

我希望能够在管理界面中创建一个报价,这样当我填写了订单项的数量和单价时,它会填写当我点击时,“extend_price”是两者的乘积。我认为这需要在其中添加一些 AJAX。

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

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. I connect the two using an intermediate model "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. I think it requires adding some AJAX in there.

Annotated picture describing what I would like

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

芸娘子的小脾气 2024-09-22 16:34:31

有关如何在模型管理中包含 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));
        }
    });
});

类似的东西。

只是添加了货币格式功能,以防您想使用它。

Info on how to include js in your model admin:
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.

尬尬 2024-09-22 16:34:31

您不会轻易将该字段放入更改列表中,因为它属于正在编辑的模型的另一个模型。不过,您可以将 through 模型作为内联包含在该模型下方,然后您可以简单地编写一些 JS,它接受两个输入字段并生成所需的输出值,并将其放入 through 模型上的适当字段中包含在内联中。

或者,编写一个不依赖管理员的自定义视图;o)

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.

Or, write a custom view that doesn't lean on the admin ;o)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文