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

查看:39
本文介绍了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 时,用 Item 更新 SaleDetail.unit_price.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天全站免登陆