Seeking help in designing django models

69 Views Asked by At

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 :

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

wells - class to hold well's attributes.

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

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

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

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.

1

There are 1 best solutions below

1
On BEST ANSWER

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.

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)