Search code examples
djangoformsmodels

how to represent some kind of matrix data tables in django models?


Need some help really,

How to represent a kind of data like the one below, using django models? , so that I can build forms to add/edit/delete entries, and build some reporting efficiently?

This is for illustrative purpose, a table of numbers of disease occurences on each month of year, based on disease types, age_group of patient which further divided into male and female, and then into recurring and new occurences.

occurence entries on parent group would be aggregates of its children.

year : 2014 month : January
------------------------------------------------------------------------------
                                |   age_group1    |   age_group2   |  etc .. 
                                | male   | female |male   | female |           
                                | rec|new|rec|new |rec|new|rec|new |                                
----------------------------------------------------------------------
infectious disease group        : 11   5   
    viral disease subgroup      :  8   0                             etc ...
       viral_disease1           :  4   0
       viral disease2           :  4   0
    bacterial disease subgroup  :  3   5
       bacter_disease1          :  1   4
       bacter_disease2          :  2   1
degenerative disease group      :
    neural_disease subgroup     :
        neur_disease1           :
        neur_disease2           :
     ... etc ..         

Solution

  • Here's one design you could work with. It allows for flexibility with defining the diseases as well as the reports. If you wanted, you could create a new Report model with the start and end fields on that and a ManyToMany to the DiseaseReport model. This design assumes you've given the data in bulk. If you're given the individual patient information, then this will have to be modified.

    class AgeGroup(models.Model):
        pass
    
    class PatientGroup(models.Model):
        NEW = 0
        RECURRING = 1
        PATIENT_STATES = (
            (NEW, "New"),
            (RECURRING, "Recurring"),
        )
        age_group = models.ForeignKey(AgeGroup)
        is_male = models.BooleanField()
        state = models.SmallIntegerField(choices=PATIENT_STATES, default=NEW)
    
    DiseaseGroup(models.Model):
        parent_disease = models.ForeignKey(DiseaseGroup, null=True, blank=True)
        name = models.CharField(max_length=100)
    
    Disease(models.Model):
        disease_group = models.ForeignKey(DiseaseGroup)
        name = models.CharField(max_length=100)
    
    DiseaseReport(models.Model):
        start = models.DateField()
        end = models.DateField()
        disease = models.ForeignKey(Disease)
        patient_group = models.ForeignKey(PatientGroup)
        cases = models.IntegerField(default=0)