Django-根据另一个字段更新模型字段 [英] Django - Update model field based on another field

查看:687
本文介绍了Django-根据另一个字段更新模型字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django和Python的新手,我想做一些以前在Java EE中经常做的事情.

I am new to Django and Python and I want to do something I used to do very often in Java EE.

请考虑以下模型(仅相关类):

Consider the following model (only relevant classes):

class Item(models.Model):
    name = models.CharField(max_length=40)
    default_price = models.DecimalField(max_digits=6, decimal_places=2, default=50)

    def __unicode__(self):
        return self.name


class SaleDetail(models.Model):
    item = models.ForeignKey(Item)
    deposit = models.ForeignKey(Deposit)
    quantity = models.PositiveIntegerField()
    unit_price = models.DecimalField(max_digits=6, decimal_places=2)
    sale = models.ForeignKey(Sale)

    def item_updated(self, value):
        if self.unit_price == None:
            self.unit_price = value

我想做的是,每次Item添加到SaleDetail时,如果SaleDetail是新的或未设置unit_price,则用Item.default_price更新SaleDetail.unit_price.

What I want to do is that each time an Item is added to SaleDetail, update SaleDetail.unit_price with Item.default_price if SaleDetail is new or no unit_price is set.

我以前在Java POJO中所做的就是在setter方法中包含此逻辑.我尝试使用python属性封装item属性,但是Django直接在内部更新字段,因此这会破坏某些自动功能.

What I used to do in Java POJOs was to include this logic in the setter method. I've tried using python properties to encapsulate the item property, but Django updates the field directly under the hood so this would break some automatic functionallity.

我还尝试将ForeignKey子类化为接受回调函数,但是我找不到在容器类上调用方法的方法.

I've also tried to subclassing ForeignKey to accept a callback function but I couldn't find a way to call a method on the container class.

我想这样做,所以我可以为UI提供默认值,但是我不想在视图逻辑中包含此逻辑,因为从概念上讲,我认为该逻​​辑应该在模型上(服务器端)

I want to do this so I can provide a default for the UI but I don't want to include this logic in the view logic, since conceptually I think this logic should be on the model (Server side)

在这种情况下,其他用途是更新每个销售明细和销售的总计.我想在用户决定保存销售之前进行计算,因此保存信号将不起作用.

Other use in this case would be to update totals for each sale detail and sale. I would like to calculate this BEFORE the user decides to save the sale, so save signals would not work.

谢谢!

推荐答案

单价"实际上是两个不同字段的函数.因此,我倾向于这样写:

The "unit price" is a literally a function of two different fields. As such I would tend to write it like this:

class Item(models.Model):
    name = models.CharField(max_length=40)
    default_price = models.DecimalField(max_digits=6, decimal_places=2, default=50)

    def __unicode__(self):
        return self.name

class SaleDetail(models.Model):
    item = models.ForeignKey(Item)
    deposit = models.ForeignKey(Deposit)
    quantity = models.PositiveIntegerField()
    entered_unit_price = models.DecimalField(max_digits=6, decimal_places=2, default=None)
    sale = models.ForeignKey(Sale)

    @property
    def unit_price(self, value):
        if self.entered_unit_price is None:
            return self.item.default_price
        else:
            return self.entered_unit_price

    @unit_price.setter
    def unit_price(self, value):
        self.entered_unit_price = value

然后像这样使用它:

print(sd.unit_price)
sd.unit_price = 500 
sd.save()

这篇关于Django-根据另一个字段更新模型字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆