【问题标题】:Display a dynamically updated quantity BUT store the older versions显示动态更新的数量但存储旧版本
【发布时间】:2020-09-23 15:13:31
【问题描述】:

我正在为一个农民开发一个网络应用程序,更准确地说是一个永久种植者。这意味着他正在处理各种种子和植物。该应用程序的目的是在数据库中存储有关他的花园的数据。一旦收集到足够的数据,应用程序将提供一个框架来分析数据。

目前我正在开发第一个功能:将数据存储在数据库中并在网页上显示。

让我们专注于这个问题的主题。

花园有几块田地。一个字段可以包含多个植物。植物在一段时间内可以有多种状态(种子、植物、花朵、可收获)。

当植物达到某种状态时,我们需要存储特定的信息:

  • 国家
  • 观察状态的日期(和其他与时间相关的数据)
  • 数量(从种子状态到开花状态,我们可能会因多种原因而损失)

到目前为止一切都很好,没什么特别的。

现在植物可以在一块田地里生长直到达到特定状态,然后再种植到另一块田地里直到收获。

例如,从种子到发芽状态在 3 号托盘中生长的 12 根胡萝卜。

在发芽状态,2 根胡萝卜没有成功。农民现在打算恢复种植胡萝卜,而不是在第 3 号托盘中,而是在第 1 号田地中

在模型中,假设“state_plant_table”有 2 个条目:

胡萝卜 - 12 - 种子 - 托盘 n°3 胡萝卜 - 10 - 胚芽 - 第 1 场

你可能会看到它的到来。

现在让我们说... 1 号田地没有足够的空间容纳 10 根胡萝卜,只能放 8 根。所以他只是把剩下的 2 放在了场上 - 第 2 场。

我们现在有 胡萝卜 - 12 - 种子 - 托盘 n°3 胡萝卜 - 8 - 胚芽 - 第 1 场 胡萝卜 - 2 - 胚芽 - 第 2 场

现在,我们将为每个字段、托盘或 w/e 显示 HTML 表格。当您点击一个字段时,您会看到其中所有植物的详细信息。

对于字段 n°1,我们将有: 胡萝卜 - 8

对于第 n°2 场,我们将有: 胡萝卜 - 2

不幸的是,对于 3 号托盘,我们会: 胡萝卜 - 12 但我们应该有 0(如果 0 => 当然从显示中排除)。

我现在正在为我的流程的理论设计而苦苦挣扎...欢迎任何提示、提示和建议!

我考虑了一个“父”数量和一个“子”数量,其中初始数量将存储在“plant_table”中作为“父”数量,“子”数量将存储在“state_plant_table”中 - 数量是与植物本身相比,它与被观察的状态更相关。

我觉得这是正确的方法,但我也没有设法将推理推向终点。

【问题讨论】:

    标签: database django-models database-design


    【解决方案1】:

    用“父母”和“孩子”推理是正确的方法之一。

    存储数量实际上有 3 种性质:

    • 在特定状态下观察到的植物数量 (quantite_etat)
    • 田间实际植物的数量(数量)
    • 来自同一亲本的植物数量 (quantite_lot)
    models.py
    
    class EtatLot(models.Model):
        id = models.AutoField(primary_key = True)
        id_lot = models.ForeignKey('Lot', on_delete = models.PROTECT, verbose_name = 'Lot', related_name = 'lot_parent', null = True, blank = True)
        etat = models.CharField(verbose_name = 'État', max_length = 50, null = True, blank = True)
        quantite = models.PositiveSmallIntegerField(verbose_name = 'Quantité', null = True, blank = True)
        quantite_etat = models.PositiveSmallIntegerField(verbose_name = 'Quantité relatée', null = True, blank = True)
    
    class Lot(models.Model):
        id = models.AutoField(primary_key = True)
        quantite_lot = models.PositiveSmallIntegerField(verbose_name = 'Quantité', null = True, blank = True)
    

    显示的数量是来自 quantite 的数量。用于分析数据的量是来自quantite_etat的量。

    示例

    假设我们在 1 号田地有 12 个花椰菜,农民想在 2 号田地种植 10 个。

    在数据库中我们有:

    Lot table
    id : 1   quantite_lot : 12
    
    
    EtatLot table
    id : 1   id_lot : 1   etat : Seed   quantite : 12   quantite_etat : 12
    id : 2   id_lot : 1   etat : Germ   quantite : 12   quantite_etat : 12
    

    在操作结束时,我们应该有这个:

    Lot table
    id : 1   quantite_lot : 2
    
    
    EtatLot table
    id : 1   id_lot : 1   etat : Seed   quantite : 12   quantite_etat : 12   field : fieldn°1
    id : 2   id_lot : 1   etat : Germ   quantite :  2   quantite_etat : 12   field : fieldn°1
    id : 3   id_lot : 1   etat : Plan   quantite : 10   quantite_etat : 10   field : fieldn°2
    

    对于这个操作,quantite_lot 是无关紧要的。但是我存储它是为了做一些库存检查:你不能种植更多的植物。

    这就是我实现上表的方式:

    • 从 lot_parent 的最后一个子节点获取数量(数量)
    • 用它和农民在表单中添加的值之间的差来更新这个值,同时更新父节点中 quantite_lot 的值
    • 将表单的值存储在即将添加到数据库中的条目的 quantite 和 quantite_etat 中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-16
      • 2013-12-28
      • 2018-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-20
      相关资源
      最近更新 更多