1

This is a fairly quick question regarding schema design for a Django project.

Basically, we have a series of monthly reports from different departments that are aggregated into a single report with some pretty charts (we're probably going to use Google Visualization API for that, but I'm open to other suggestions, if you think something else integrates nicely with Django).

Each department is responsible for filing their own figures for their part of the report. We'll probably be using the Django admin for entering in those figures, since it doesn't have to be pretty, it's just to get some numbers in each month.

I'm assuming the better way here is to have an abstract Report model, inherit this for each department having a separate Model, with any specific fields overridden, and then have a DateField on each one as well.

Having a month as the parent object, and descending the reports from that - that's a silly approach, right?

Also, what's the best way of enforcing it so that they can only submit their figures once? I could have a separate month and year field, I guess and enforce a unique on that field, but I was hoping to use the inbuilt DateField, but what's the best way to enforce month and year uniqueness? Should I use the new model validation feature for that?

Cheers, Victor

victorhooi
  • 16,775
  • 22
  • 90
  • 113

1 Answers1

1

A Django model field can have an option unique_for_month and unique_for_year. The value of that option is a DateTime or Date field in the model.

E.g.

class Report(models.Model):
    submit_date = models.DateField()
    department_id = models.IntegerField(unqiue_for_month=date)
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • This seems to be completely incorrect. Using the sqlite3 backend at least, `unique_for_month` refers to just that: a unique month. As in, put in two Decembers and the admin site spits at you, regardless of their different years. – Jjed Dec 28 '11 at 02:53
  • From the documentation: "*For example, if you have a field title that has `unique_for_date="pub_date"`, then Django wouldn't allow the entry of two records with the same `title` and `pub_date`. This is enforced at the Django admin-form level but not at the database level.*" So per documentation, it should work as expected, or maybe you expected something else? – Felix Kling Dec 28 '11 at 09:28