I'm using MySQL with Django and unable to see the data returned from the query executed within my model manager.
The page is rendered with the table, borders and pagination is working, however, none of the fields values are appearing in the table.
I'm guessing there is a step needed between returning the query results and presenting it in html but I'm stumped.
For context, I am setting up my manager so I can execute more sophisticated queries than what is offered with Django.
I followed some examples for using model Managers with a fairly simple query as a start - .. one of many references I have researched outside of this site: https://docs.djangoproject.com/en/dev/topics/db/managers/#custom-managers
After spending a lot of time searching I trust someone here can help. Thanks in advance!!
Here is the model manager :
class ElectionsManager(models.Manager):
def is_active(self):
from django.db import connection
cursor = connection.cursor()
cursor.execute("""
SELECT *
FROM
newvoterproject.fullvh vh1
WHERE
vh1.city = 'Glocester' and
vh1.current_party = 'd'
group by
vh1.city,
vh1.street_name,
vh1.street_name_2,
vh1.street_number,
vh1.unit
;""")
result_list = cursor.fetchall()
return result_list
and here a snip of the model:
class Election(models.Model):
voter_id = models.CharField(primary_key=True, max_length=25)
last_name = models.CharField(max_length=50, blank=True, null=True)
first_name = models.CharField(max_length=50, blank=True, null=True)
middle_name = models.CharField(max_length=50, blank=True, null=True)
current_party = models.CharField(max_length=50, blank=True, null=True)
street_number = models.CharField(max_length=50, blank=True, null=True)
street_name = models.CharField(max_length=50, blank=True, null=True)
street_name_2 = models.CharField(max_length=50, blank=True, null=True)
unit = models.CharField(max_length=50, blank=True, null=True)
city = models.CharField(max_length=50, blank=True, null=True)
state = models.CharField(max_length=50, blank=True, null=True)
zip_code = models.CharField(max_length=50, blank=True, null=True)
zip_code_4 = models.CharField(max_length=50, blank=True, null=True)
precinct = models.CharField(max_length=50, blank=True, null=True)
status = models.CharField(max_length=50, blank=True, null=True)
objects = ElectionsManager() # model manager
class Meta:
managed = False
verbose_name = 'Election'
verbose_name_plural = 'Elections'
db_table = 'fullvh'
def __str__(self):
return '%s %s' % (self.first_name, self.last_name)
Calling the model manager from the view:
def vhistfun(request):
election_table = Election.objects.is_active()
paginator = Paginator(election_table , 25) # Show 25 contacts per page - may want to change this to READ 25 at a time...
page = request.GET.get('page')
try:
electpage = paginator.page(page)
except PageNotAnInteger:
electpage = paginator.page(1)
except EmptyPage:
electpage = paginator.page(paginator.num_pages)
context = {'electpage': electpage,
}
return render(request, 'elections/electable.html', context)
.. and the html snip processing the results
{% for elect in electpage %}
<tr id="voterrowclass" class="">
<td> {{ elect.first_name|lower|capfirst }} </td>
<td> {{ elect.last_name|lower|capfirst }} </td>
<td> {{ elect.current_party}} </td>
<td> {{ elect.street_number}} {{ elect.unit}} </td>
<td> {{ elect.street_name|lower|capfirst}} {{ elect.street_name_2|lower|capfirst}} </td>
<td> {{ elect.city|lower|capfirst}} </td>
</tr> <!-- # model data sent from view -->
{% endfor %}