On Thursday 15 November 2012 17:12:09 Carsten Fuchs wrote:
> using Django 1.3 (soon 1.4), we use models like these:
> class Staff(models.Model):
>      name = models.CharField(max_length=80)
> class Period(models.Model):
>      staff = models.ForeignKey(Staff)
>      begin = models.DateField()
>      end   = models.DateField(editable=False)    # see below
>      # The actual data that describes this period,
>      # e.g. department, salary, ...
>      department = models.CharField(max_length=60)
>      class Meta:
>          ordering = ['begin']
> 
> There are normally several Period instances for each staff member, and
> the set of periods for one staff member form a chronological sequence:
> 
> A period begins at the day given in 'begin'.
> It ends at the day before the *next* period begins.
> 
> Thus, in our original design, there was no field 'end' in the Period
> model, because logically, it was not needed. (The last period is
> "open-ended".)
> 
> However, we found that many queries are very difficult to build without
> the 'end' (such as "Find all staff members in department X on November
> 1st"), and thus are planning to add it as shown above.
> 
> 'end' should always be computed automatically as the day before the
> begin of the next period, but I don't know where this is best
> implemented in the admin interface:
> 
> class PeriodInline(admin.TabularInline):
>      model = Period
> class StaffAdmin(admin.ModelAdmin):
>      inlines = [ PeriodInline ]
> 
> (Note that it is not enough to consider the Period that changed --
> rather, it is the 'end' in *another* period (the one "before" it) that
> must be modified and saved, too.)
> 
> What is the proper hook or where the best place to do this?

Why do you want to do this only in the admin interface?
Its a generic thing: every time you save/change a period you should set the 
end-date of the previous one. So I would do this with a post-save hook (aka 
signal) directly in the models.py. Or maybe even subclass your models save-
operation.

Have fun,

Arnold

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to