当并非所有情况下都需要所有字段时,如何为用户输入构建Django模型

2024-03-28 17:34:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用新功能扩展现有的django应用程序。 问题基本上是这样的。你知道吗

抱歉,如果已经问过这个问题,我不确定要搜索什么来找到解决方案。你知道吗

我有一个'商店'模型,它包含了许多与'部门'模型的关系,不是所有商店都有相同的部门。你知道吗

我需要跟踪每个商店的每日部门数量,我需要一个用户表单来输入所有这些信息。你知道吗

我不知道是应该用所有可能的部门创建一个模型,还是应该用一个部门和权重创建一个模型,然后为每天创建尽可能多的对象作为部门。你知道吗

扩展能力在这个应用程序将是非常重要的未来,我想开始的权利。你知道吗

代码如下。你知道吗

商店部门

class Department(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, unique = True)
    short_name = models.CharField(max_length=32)
    description = models.CharField(max_length=255)
    def __str__(self):
            return self.name;

体积数据选项(有问题的型号)

选项1,一个型号,所有型号:

class VolumeData(models.Model):
    id = models.AutoField(primary_key=True)
    store = models.ForeignKey(stores.models.Store, on_delete=models.DO_NOTHING)
    date = models.DateField(auto_now_add=True,null=False,blank=False)
    produce = models.DecimalField(decimal_places=2,Null=True)
    dairy = models.DecimalField(decimal_places=2,Null=True)
    deli = models.DecimalField(decimal_places=2,Null=True)
    meat = models.DecimalField(decimal_places=2,Null=True)
    seafood = models.DecimalField(decimal_places=2,Null=True)
    coffee = models.DecimalField(decimal_places=2,Null=True)
    frozen = models.DecimalField(decimal_places=2,Null=True)

选项2,一个模型,但我需要更多的对象。你知道吗

class VolumeData(models.Model):
    id = models.AutoField(primary_key=True)
    store = models.ForeignKey(stores.models.Store, on_delete=models.DO_NOTHING)
    date = models.DateField(auto_now_add=True,null=False,blank=False)
    department = ForeignKey(Departmnet, blank=False)

我觉得选项2会更灵活,但我担心它会创建多少额外的对象。你知道吗

然而,选项1会有很多我不需要的空值,我不确定这是否更糟,它也会在部门中烘烤,这可能很复杂?你知道吗

部门列表不会很动态,我希望每年更新不到一次,而且最终用户不太可能需要修改这些信息。你知道吗


Tags: 对象模型idfalsetruemodelmodels选项
1条回答
网友
1楼 · 发布于 2024-03-28 17:34:44

我也喜欢选项2,但我认为您需要更进一步,为每个部门销售的不同商品创建一个类:

class Items(models.Model):
    id = models.AutoField(primary_key=True)
    product_SKU = models.CharField(max_length=15) # set max_length as needed
    product_name = models.CharField(max_length=50) # set max_length as needed
    number_sold = models.DecimalField(decimal_places=2,Null=True)

有了这些信息的深度,你就可以更深入地进行报告、人工智能预测、机器学习等,只要我的两分钱。你知道吗

相关问题 更多 >