Reputation: 5402
I'm trying to retrieve a list of tags that share the same name
as a django Country. (i will be throwing it into my autocomplete search). What I have isn't working:
View:
from django_countries.countries import COUNTRIES
...
@login_required
def country_tags(request):
result = {}
tags = Tags.objects.all()
countries = list(COUNTRIES)
for tag in tags:
for country in countries:
if country.name == tag.name:
result[tag.name] = tag.name.title()
return HttpResponse(json.dumps(result))
Can't quite figure out why this isn't working. Am I wrong to reference country.name
?
Upvotes: 0
Views: 857
Reputation: 5144
COUNTRIES
is just a list of 2 elements tuples - there is no name
property. You should do something like country[1] == tag.name
.
Upvotes: 0
Reputation: 174624
Here is a version that should work. COUNTRIES
is a 2-tuple tuple.
countries_only = [x[1] for x in COUNTRIES]
tags = Tag.objects.filter(tag.name__in=countries_only)
results = {}
for t in tags:
results[t.name] = t.name.title()
Upvotes: 3