Just when I was getting comfortable with simple views and templates, I've walked into a wall on my first attempt to do something more complicated. Any help anyone can give me will have my undying respect.
Let's say my application is a site for youth sports. I have a table of cities, teams and players. I've written a simple view that gives a user a list of cities they can see team and player information for. What I want to have happen when the users clicks the city is for them to be taken to a page that lists each team in that city with a count of the number of players on each team. This is a simple group and count in SQL. So I've created a manager that is pretty much a <a href="http://www.djangoproject.com/documentation/model-api/#custom- managers">cut and paste job from the docs.</a> The models and the custom manager I have look like this: class mastercitylist(models.Model): city = models.CharField(blank=True, maxlength=100) cityslug = models.SlugField(prepopulate_from=("city",)) class Admin: pass def get_absolute_url(self): return "http://127.0.0.1:8000/data/sports/cities/%s/" % self.cityslug def __str__(self): return self.city class masterteamlist(models.Model): name = models.CharField(maxlength=150) name_slug = models.SlugField(prepopulate_from=('name',)) class Admin: pass def __str__(self): return self.name class players(models.Model): ... city = models.ForeignKey(mastercitylist) team = models.ForeignKey(masterteamlist) class Admin: pass class PlayerManager(models.Manager): def with_counts(self, city): from django.db import connection cursor = connection.cursor() cursor.execute(""" SELECT sports_players.city_id, sports_players.team_id, count(sports_players.team_id) FROM sports_players, sports_mastercitylist WHERE sports_players.city_id=sports_mastercitylist.id AND sports_mastercitylist.cityslug = %s GROUP BY 1, 2 ORDER BY 1""" % city) result_list = [] for row in cursor.fetchall(): counts = self.model(city_id=row[0], team_id=row[1]) counts.num_responses = row[2] result_list.append(counts) return result_list class PlayerCounts(models.Model): team = models.ForeignKey(masterteamlist) city = models.ForeignKey(mastercitylist) objects = PlayerManager() Here's the line from my urls.py specific to this problem: (r'^data/sports/cities/(?P<city>\w*)/$', 'data.sports.views.teamcountview'), And here's the view: def teamcountview(request, city): teamcountlist = PlayerCounts.objects.with_counts(city) return render_to_response('sports/teamcountview.html', {'teamcountlist': teamcountlist}) Now, when I put this all together and go to data/sports/cities/ clearwater I get IndexError: list index out of range. Now, if I take the SQL I wrote and run it in MySQL with a valid cityslug in the where clause, it works. I get a list of teams in that city and a count of the players on those teams. Which leads me to believe that where this is going wrong is that I'm not passing the cityslug context from the url to the custom manager correctly, and I'm getting nothing back. But I have no idea where I've gone wrong, or how to fix it. I've blindly stumbled to get to this point. If you see where I've gone wrong, or can tell me a different/easier way than I've cobbled together, I would greatly appreciate it. Matt Matthew Waite www.mattwaite.com --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---