【问题标题】:How can I create a model that represents a spreadsheet?如何创建代表电子表格的模型?
【发布时间】:2021-09-30 15:15:08
【问题描述】:

我正在尝试构建一个表格样式的应用程序,以允许跟踪可以添加到品牌网站的电子表格中的项目。如果使用电子表格,我的表格将如下所示:

Customer Name Customer Address Producer
Mark 233 Main St Greg
Company Date Ordered Rep Date Received Cost Quote Number
A 7/20/21 John 7/25/21 500 JDHP
B 7/20/21 Mary
C 7/23/21 John 7/25/21 1500 584D
D 7/18/21 Mary 7/22/21 400 J5HP

实际上,我的想法是我有一个模型来容纳每个客户的不同报价。我有 2 类公司(公共和私人),每个都会被跟踪,所以我设想一个包含这三种小表格(客户信息、私人公司报价和上市公司报价)的大型表格。无论我们是否与他们联系以获取报价,我都会在每张表中包括每家公司,以便我们知道如果客户要求更多报价,哪些选项仍然可用。

我一直在将 django 表单集视为一种可能的选择,但并不完全了解它们是如何工作的。我观看了一些教程并通读了文档,但似乎这些只是在表格中所有完整的输入之后添加一个空白输入。这是对表单集如何工作的正确解释吗?或者他们会有效地允许我在一个更大的表单中嵌套多个表单吗?其次是我将如何实现这个模型?我为列表中的每家公司尝试了 company_date_ordered、company_rep、company_date_received 等,但出现了很多冲突错误。

【问题讨论】:

    标签: django django-models formset


    【解决方案1】:

    欢迎来到网络开发。

    几点:

    1. 有人说,"Applications age like fish, data ages like wine" - 这意味着最好将注意力集中在数据模型上,而不是关注应用程序代码本身
    2. 电子表格本身就是表格 - 这意味着您不会构建表示电子表格的模型,而是构建表示电子表格的一行(对象)或每行的元素(对象)的模型

    话虽如此,鉴于您的示例,让我们考虑一下我们正在使用哪些对象:

    1. 您的第一个“电子表格”看起来可能是 CustomerProducer 表的组合 - 对于此示例,我们将其简化为仅包含字段 producerCustomer
    2. 您的第二个电子表格有点复杂,它似乎加入了Company,类似于Order 和可能的Representative(甚至更多) - 让我们将其分解为各个部分:

    models.py

    class Customer(models.Model):
     
        """ 
            this is a simple model that represents a customer, 
            it doesn't have any relations to other tables 
        """
    
        name = models.CharField(...)
        address = models.CharField(...)
        producer = models.CharField(...)
    
    
    class Order(models.Model):
    
        """
            this table represents orders, 
            it will have relations to other tables, add them as needed
        """
       
        # fields:
        date_ordered = models.DateField(...)
        date_received = models.DateField(...)
        quote_number = models.CharField(...)
    
        """
            an Order can only be associated to one Company 
            but a Company can have many Orders
            therefore we will use a ForeignKey
            the same goes for Representative
        """
       
        # relations:
        company = models.ForeignKey("Company", ..., related_name = "orders")
        representative = models.ForeignKey(...)
        
    
    
    class Company(models.Model):
    
        """
            this model represents a Company
            its related to Orders,
            but the relation is defined on the Orders table
        """
    
        name = models.CharField(...)
    
    
    class Representative(models.Model):
    
        ...
    

    花一些时间思考您的实体是什么以及它们之间的关系。一旦您的数据结构形成良好,您就可以开始构建界面供用户查看、编辑和添加到您的表格 - 表单是一个很好的开始方式,但它们肯定不是唯一的选择。祝你好运!

    【讨论】:

    • 这很有意义。我已经开始了解这一点,但是添加 related_name 字段以及代码的布局确实有助于我理解这一点。非常感谢!
    • 没问题 - 还有两种其他类型的关系 OneToOneManyToMany 请务必查看文档以了解更多信息,看看哪种关系最适合您的模型。
    猜你喜欢
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 2020-10-20
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    相关资源
    最近更新 更多