On Sep 8, 7:29 pm, Slammer <walter.kirkw...@gmail.com> wrote: > FYI, I'm a beginner, I'm having trouble with custom methods, I'm > using an existing database which is used for a fantasy football > site, the primary data return is a list of players for a particular > team, I've done that fine, but the score for those players is in a > different table, So to return the list of players with their current > scores I tried to create a custom method as such in my models.py > > class Player(models.Model): > p_keyid = models.IntegerField(primary_key=True) > p_lname = models.CharField(max_length=135) > #bla bla bla more fieldnames > ... > > def ptot(self): > from django.db import connection > cursor = connection.cursor() > cursor.execute(""" > SELECT sum(s_total) s_total from score where s_pno = > %s""", [self.p_keyid]) > #return [row[0] for row in cursor.fetchone()] > #return [self.__class__(*row) for row in cursor.fetchone()] > return u'S=%s' % (self.__class(*row).s_total) > > As you can see I've tried a bunch of ways to return the related score > total, none have worked. > > in my views.py I have > def search_roster(request): > if 'q' in request.GET and request.GET['q']: > q = request.GET['q'] > player = Player.objects.filter(p_uffl__icontains=q) > return render_to_response('search_results.html', > {'player': player, 'query':q}) > else: > return render_to_response('search_form.html', > {'error': True}) > > in my search_results.html I have > <p>Team Listing for: <strong>{{ query }}</strong></p> > {% if player %} > <p>Found {{ player|length }} player{{ player|pluralize }}.</p> > <ul> > <table border=1> > <tr><td>Pos</td><td>Name</td> > <td>NFL</td><td>Value</td> > <td>Score</td></tr> > {% for player in player %} > <tr><td>{{ player.p_pos }}</td><td>{{ player.p_lname }}</td> > <td>{{ player.p_nfl }}</td><td>{{ player.p_val }}</td><td> > {{ player.ptot }}</td></tr> > {% endfor %} > </table> > </ul> > {% else %} > <p>No players matched your search criteria.</p> > {% endif %} > > My questions are this, when I just want a single field return for a > custom method how do you form the return statement, in my case I'm > wanting what ever I alias the sum of s_total. After which how does > that value get successfully called from the html template. All of it > works but I never get a return value > > or is there an easier way to do this? thanks in advance
Why do you need to turn it into a field at all? Why not just return cursor.fetchone()[0] which will return the first (only) field in the returned data, which can be used directly in your template. Also note that you can probably avoid all this by using version 1.1's new aggregation features, since all you want is the total of score.s_total for a relevant Player. As long as you have a ForeignKey from the Score model to Player, you could just do this in your view: from django.db.models import Sum players = Player.objects.filter(p_uffl__icontains=q).annotate (ptot=Sum('score__stotal')) This generates the ptot values for every player in one go, which is much more efficient as well as neater. (And please, consider using better field names - unless this is a legacy database, there's no reason to prefix them with the table initial, and no reason not to give them properly descriptive names.) -- DR. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---