Question

I'm building a small reporting system for a group of IT projects and can't figure how do put this together at django models level.

The idea is fairly simple:

I have a Dictionary of RAG indicators (green, yellow, red and so on):

class DicProjectStatusRAGIndicator(DicCoreBase):
    text_marker = models.CharField(max_length=64)
    color_marker = models.CharField(max_length=6)
    icon = models.FileField(upload_to='RAG_icons')

DicCoreBase is an abstract model that adds a few additional logistical attributes to all Dictionary items.

Then, there are the actual RAG metric items (such as 'progress', 'timelines', 'scope definition' and such):

class DicProjectStatusRAGReportItem(DicCoreBase):
    name = models.CharField(max_length=32)
    description = models.TextField()

on top of these, there are a few free text items that allow people add comments and description to their status records:

class DicProjectFreeTextReportItem(DicCoreBase):
    name = models.CharField(max_length=32)
    description = models.TextField()

So, say, for example, we have two Indicators: 'green' and 'red', two metric items: 'progress' and 'quality' and one free text: 'improvements done'. A project report record would then be something like:

'progress' -> 'green'
'quality' -> 'red'
'improvements done' -> 'fixed all major defects'

bound to a particular 'Project' model.

How can this modeled with standard django modeling tools?

Was it helpful?

Solution

1. Based on your bottom example, metric items have an Indicator. That should be a ForeignKey relationship:

class DicProjectStatusRAGIndicator(DicCoreBase):
    text_marker = models.CharField(max_length=64)
    color_marker = models.CharField(max_length=6)
    icon = models.FileField(upload_to='RAG_icons')

class DicProjectStatusRAGReportItem(DicCoreBase):
    name = models.CharField(max_length=32)
    description = models.TextField()
    indicator = models.ForeignKey(DicProjectStatusRAGIndicator)

Assuming 'fixed all major defects' is the description field of Free Text, then DicProjectFreeTextReportItem is fine.

2. Since a project report record can have multiple metric items and free text items, that should be a Many-to-many relationship:

class ProjectReportRecord(models.Model):
    project = models.ForeignKey(Project, related_name='reports')
    metrics = models.ManyToManyField(DicProjectStatusRAGReportItem)
    free_texts = models.ManyToManyField(DicProjectFreeTextReportItem)

Now, referring to your example above and our Django models we've made, we can get to that information like so:

p = Project.objects.get(pk=1)
record = p.reports.all()[0]
print record.metrics.all().values_list('name', flat=True)
# prints ['progress', 'quality']
print record.metrics.get(name='progress').indicator.text_marker
# prints 'green'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top