寻求帮助设计Django模型

2024-06-28 22:09:59 发布

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

我正在为一个我正在工作的项目寻找一些关于django模型的反馈来吧。那又怎样我正在建立一个文档数据库,其中的文档可以分为3类-GTO,EWR和QPR.每个这些文件中嗯,每个人可以有多个文档与是的。是的用户可以上传和查看与好吧,给你这是我的设计:

basedoc—保存文档属性的类,将用作基类。你知道吗

wells类来保存well的属性。你知道吗

GTO-继承自basedoc,并使用外键与wells链接。你知道吗

EWR-继承自basedoc,并使用外键与wells链接。你知道吗

QPR-从basedoc继承,并使用外键与wells链接。你知道吗

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

我最初使用basedoc作为一个抽象基类,但做了更改,因为我想将所有文档的列表作为一个抽象基类返回给用户请看帮我改进一下设计。谢谢. 你知道吗


Tags: 文档selfmodels基类lengthmax外键class
1条回答
网友
1楼 · 发布于 2024-06-28 22:09:59

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

您可以使用以下选项:

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

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

相关问题 更多 >