2π
Iβll shorten the code to the specific part that was very likely the issue:
query = table.objects.values(genus, species)
valueList = []
for q in query:
species = ' '.join(q.values())
valueList.append(species)
As you read in the documentation, values()
will return a list of dictionaries, like this:
[{'genus_hetr': 'foogenus', 'species_hetr': 'foospecie'},
{'genus_hetr': 'bargenus', 'species_hetr': 'barspecie'}]
You would then iterate through that list. On each iteration, q
is one of the dicionaries. So far so good. Then, the issue:
' '.join(q.values())
A dictionary has no ordering. So this would join all values alright, but in an arbitrary order. Even those table you thought were working would have broken randomly at a later point.
So how to fix that? Well, like you did, using values_list
returns tuples, which are ordered. Another option would have been to not rely on the order, but use the dictionary as such, that is, addressing it by key:
species = '%s %s' % (q[genus], q[species])
By the way, for such a simple case you probably want to use a list comprehension. The whole loop can be written like this:
valuesList = ['%s %s' % (q[genus], q[species]) for q in query]
0π
As the docs would have it, instead of returning a dictionary (that was back to front for that one table), there is an option to return a value list. This came out the right way round and so the queryBuilder() function works like this:
views.py
def queryBuilder(table, column_str):
genus = 'genus_' + column_str
species = 'species_' + column_str
filterSp = {species: 'sp.'}
query = table.objects.values_list(genus, species).distinct().exclude(**filterSp)
valueList = []
for q in sorted(query):
species = ' '.join(q)
valueList.append(species)
return(valueList)
I still donβt know why the previous query was coming out the wrong way around for that one table which is annoying.
- [Answered ]-Sentry-youtrack plugin: PicklingError: Can't pickle <type 'generator'>: attribute lookup __builtin__.generator failed
- [Answered ]-Javascript post django form gives csrf error