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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---