I'm using Django 1.8 with a MySQL backend. I need to get two columns from several tables in a database and concatenate them, then put the sorted results on a webpage. I have this so far:
views.py
def herps(request):
#performs queries to get distinct species from the database
trQuery = queryBuilder(HetrRec, '_hetr')
opQuery = queryBuilder(HeopRec, '_heop')
valList = opQuery + trQuery
query = sorted(set(valList))
return render_to_response('herps.html', {'queries': query,})
The function to perform the query on the database looks like this:
def queryBuilder(table, column_str):
genus = 'genus' + column_str
species = 'species' + column_str
filterSp = {species: 'sp.'}
query = table.objects.order_by(species, genus).values(genus, species).distinct().exclude(**filterSp)
valueList = []
for q in query:
species = ' '.join(q.values())
valueList.append(species)
return(valueList)
So the output comes out in several table combinations as Genus Species. In one table it comes out Species Genus.
I've been through thte data to remove any characters that might affect it (leading / trailing spaces, incomplete pairs etc) but no joy.
Aucun commentaire :
Enregistrer un commentaire