Any pointers would be appreciated - I'm not too excited about storing the latest_assignment_update_id in my Assignment model!!
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?
I may be missing something obvious here, but have you tried thinking
about it the other way round? The below is just an untested hint:
class Team(models.Model):
def get_status(self):
team_status =
AssignmentUpdate.objects.filter(assignment__team=self).latest().status
return team_status.
Obviously you'd have to handle some edge cases (like non-existing
AssignmentUpdate etc.).
So rather that working your way down from Team, work your way up from
AssignmentUpdate, filter by foreign keys (check documentation for
filters that span relationships) and get your latest() set up properly
(hint: class Meta). You should not really need values_list for this
type of request.
Hope it helps
Jirka
First of all, nothing stops you from writing raw SQL query if you had
one in mind. It might be more sensible than trying to massage the ORM
too much.
If you insist on ORM, but something along these lines *might* work (I
really don't have the opportunity to test it right now, need to catch
flight :)
AssignmentUpdate.objects.order_by('-actual_report_time').values_list('assignment__team',
'status').distinct()
and then you can simply select the first entry for each team from the list.
HTH
Jirka