寻求设计django模型的帮助

时间:2021-12-14 21:09:28

I am looking for some feedback on a django model for a project I'm working on.So I'm building a document database where the documents can be split into 3 categories - GTO,EWR and QPR.Each of these documents correspond to a well.Each well can have multiple documents associated with it.The users can upload and view documents corresponding to a well.Here's my design :

我正在为我正在研究的项目寻找django模型的一些反馈。所以我正在构建一个文档数据库,其中文档可以分为3个类别 - GTO,EWR和QPR。这些文档中的每一个都对应于好吧。每个人可以有多个与之相关的文件。用户可以上传和查看与井相对应的文件。这是我的设计:

basedoc - class to hold document's attributes and will serve as base class.

basedoc - 用于保存文档属性的类,将作为基类。

wells - class to hold well's attributes.

井 - 保持井的属性的类。

GTO - inherits from basedoc and is linked with wells using foreign key.

GTO - 继承自basedoc并使用外键与井链接。

EWR - inherits from basedoc and is linked with wells using foreign key.

EWR - 继承自basedoc并使用外键与井链接。

QPR - inherits from basedoc and is linked with wells using foreign key.

QPR - 继承自basedoc并使用外键与井链接。

class basedoc(models.Model):
    docfile = models.FileField(upload_to='documents/%Y/%m/%d')
    title = models.CharField("Doc Title",max_length=50)
    pub_date = models.DateTimeField('Date published',auto_now_add=True)
    remark = models.TextField(max_length=200,blank=True)
    publisher = models.ForeignKey(User)

    def __str__(self):
        return self.title

class wells(models.Model):
    well_name = models.CharField(max_length=20)
    well_loc = models.CharField(max_length=20)

    def __str__(self):
        return self.well_name

class GTO(basedoc):
    gto = models.ForeignKey(wells)
    pass

class EWR(basedoc):
    ewr = models.ForeignKey(wells)
    pass

class QPR(basedoc):
    qpr = models.ForeignKey(wells)
    pass

I initially used basedoc as an abstract base class,but changed because i wanted to return a list of all documents to the user as a view.Please help me in improving this design.Thanks.

我最初使用basedoc作为抽象基类,但是因为我想将所有文档的列表作为视图返回给用户。请帮助我改进这个设计。谢谢。

1 个解决方案

#1


1  

You probably need to retrieve all the documents of a wells from time to time. Or you may need to move a document from GTO to EWR. To be efficient with that, I wouldn't use 3 tables but 1.

您可能需要不时检索井的所有文件。或者您可能需要将文档从GTO移动到EWR。为了提高效率,我不会使用3个表而是1个。

You can use choices :

您可以使用以下选项:

TYPE_CHOICES = (
    (1, 'GTO'),
    (2, 'EWR'),
    (3, 'QPR'),
)

class Document(models.Model):
    # ...your other fields here...
    type = models.IntegerField(choices=TYPE_CHOICES)

#1


1  

You probably need to retrieve all the documents of a wells from time to time. Or you may need to move a document from GTO to EWR. To be efficient with that, I wouldn't use 3 tables but 1.

您可能需要不时检索井的所有文件。或者您可能需要将文档从GTO移动到EWR。为了提高效率,我不会使用3个表而是1个。

You can use choices :

您可以使用以下选项:

TYPE_CHOICES = (
    (1, 'GTO'),
    (2, 'EWR'),
    (3, 'QPR'),
)

class Document(models.Model):
    # ...your other fields here...
    type = models.IntegerField(choices=TYPE_CHOICES)