Hey everyone. Is there a way to get the latest ManyToMany related model? My models.py looks like this:
class Conference(models.Model): name = models.CharField(max_length=30, unique=True) class Team(models.Model): name = models.CharField(max_length=30, unique=True) conferences = models.ManyToManyField('Conference', related_name='teams', through='ConferenceMembership') def _get_current_conference(self): return self.conferences.latest() conference = property(_get_current_conference) class Player(models.Model): name = models.CharField(max_length=15, unique=True) teams = models.ManyToManyField('Team', related_name='players', through='TeamMembership') def _get_current_team(self): return self.teams.latest() team = property(_get_current_team) class ConferenceMembership(models.Model): conference = models.ForeignKey('Conference') team = models.ForeignKey('Team') date_joined = models.DateField(auto_now_add=True) class Meta: get_latest_by = 'date_joined' class TeamMembership(models.Model): team = models.ForeignKey('Team') player = models.ForeignKey('Player') date_joined = models.DateField(auto_now_add=True) class Meta: get_latest_by = 'date_joined' This is important because Teams can change Conferences, and Players can change Teams, but their stats while on those Teams/Conferences have to remain under those Teams/Conferences. When I try and create a Team in the Admin section, the following happy error comes up: Exception Type: TemplateSyntaxError Exception Value: Caught an exception while rendering: latest() requires either a field_name parameter or 'get_latest_by' in the model So I think, "Oh, I need to move those 'get_latest_by' declarations into the top-level classes". So I change my models to look like this: class Conference(models.Model): name = models.CharField(max_length=30, unique=True) class Meta: get_latest_by = 'date_joined' class Team(models.Model): name = models.CharField(max_length=30, unique=True) conferences = models.ManyToManyField('Conference', related_name='teams', through='ConferenceMembership') class Meta: get_latest_by = 'date_joined' def _get_current_conference(self): return self.conferences.latest() conference = property(_get_current_conference) class Player(models.Model): name = models.CharField(max_length=15, unique=True) teams = models.ManyToManyField('Team', related_name='players', through='TeamMembership') class Meta: get_latest_by = 'date_joined' def _get_current_team(self): return self.teams.latest() team = property(_get_current_team) class ConferenceMembership(models.Model): conference = models.ForeignKey('Conference') team = models.ForeignKey('Team') date_joined = models.DateField(auto_now_add=True) class TeamMembership(models.Model): team = models.ForeignKey('Team') player = models.ForeignKey('Player') date_joined = models.DateField(auto_now_add=True) But when trying to add another team, I get a different error: Exception Type: TemplateSyntaxError Exception Value: Cannot resolve keyword 'date_joined' into field. Choices are: _order, conferencemembership, conferences, name, teams So I then try to access class attributes: class Conference(models.Model): name = models.CharField(max_length=30, unique=True) class Meta: get_latest_by = 'date_joined' class Team(models.Model): name = models.CharField(max_length=30, unique=True) conferences = models.ManyToManyField('Conference', related_name='teams', through='ConferenceMembership') class Meta: get_latest_by = 'conferencemembership.date_joined' def _get_current_conference(self): return self.conferences.latest() conference = property(_get_current_conference) class Player(models.Model): name = models.CharField(max_length=15, unique=True) teams = models.ManyToManyField('Team', related_name='players', through='TeamMembership') def _get_current_team(self): return self.teams.latest() team = property(_get_current_team) class ConferenceMembership(models.Model): conference = models.ForeignKey('Conference') team = models.ForeignKey('Team') date_joined = models.DateField(auto_now_add=True) class TeamMembership(models.Model): team = models.ForeignKey('Team') player = models.ForeignKey('Player') date_joined = models.DateField(auto_now_add=True) But I still get an error: Exception Type: TemplateSyntaxError Exception Value: Caught an exception while rendering: (1054, "Unknown column 'conferencemembership.date_joined' in 'order clause'") So my problem is that Conferences and Teams don't have dates to order by, but their relationships do (when a Team joined a Conference and when a Player joined a Team). I don't understand how to order those relationships to get the latest one. Am I using ManyToMany wrong? I don't see how else I would do it. --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---