On Fri, Oct 21, 2011 at 9:20 AM, Paolo <paul...@gmail.com> wrote: > An additional requirement for assignments is to keep status history, so we > can see "assignment 1 was active mon-wed, standby thu-fri then active again" > etc. > As for the query, yes that was my initial idea but I thought it was going to > be inefficient for times when I wanted to query for all assignments in a > current status. So essentially we have: > 'Load all assignments into Django. Loop through each one doing a "select top > record from assignment_update where assignment_id = this assignment", add it > to a list of matched_assignments then pass back to the caller to display" > Where what I'm trying to ask the database for is: > "Group all assignment updates by assignment and select the one with the > latest date and status X, giving me the assignment id from this record back > so I can load it into a list of filtered assignments without looking at each > one in turn". > Does that make sense? Mountain out of molehill you think? Interesting > anyway, right? ;)
I think you should store _all_ fields you need both on the Assignment and the AssignmentUpdate. In AssignmentUpdate.save, you do something like this: def save(self, *args, **kwargs): super(AssignmentUpdate, self).save(*args, **kwargs) self.assignment.field1 = self.field1 self.assignment.field2 = self.field2 self.assignment.field3 = self.field3 self.assignment.save() If you need the current values, you don't have to know anything about the AssignmentUpdate table, and it's quite easy to write efficient code for searching and showing all these values. If you need the history, sort the AssignmentUpdate instances by their creation time and compare all fields' values for differences, f.e. like this: http://dpaste.com/hold/638491/ (Note: You have to create the first AssignmentUpdate when creating the Assignment itself, otherwise you'll be unable to compare the first update to the initial values of all Assignment's fields.) Hope it helps, Matthias -- https://github.com/feincms/ Want to build your own CMS? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.