python - Django views returning only one particular field instead of entire model -
i new django. tried write view returns fields of foreign key model associated search term. instead of whole model (foreign key) getting particular field.
this view problem:
@csrf_exempt @requires_csrf_token def search_haystack(request): if request.method == 'post': search_text = request.post['search'] print search_text else: search_text = '' results_list= none if str(search_text) != "": results_dish = searchqueryset().models(dish).filter(content=autoquery(request.post.get("search", ""))) result in results_dish: print result.resto print result.resto.name print result.resto.area results_list = [{'resto_id':result.resto.id , 'name' :result.resto.name, 'area' :result.resto.area} result in results_dish] else: results = none print results_list print encodetojsonhaystack(results_list) return httpresponse(encodetojsonhaystack(results_list), mimetype = "application/json")
models:
class resto(models.model): id = models.autofield("id", max_length=100, unique=true, primary_key=true) area=models.charfield("area", max_length = 100,blank = true, null=true) name = models.charfield("name", max_length = 100,blank = true, null=true) class meta: db_table=u'resto' def __unicode__ (self): return self.name class dish(models.model): id = models.autofield("id", max_length=100, unique=true, primary_key=true) resto= models.foreignkey(resto) name = models.charfield("name", max_length = 50,blank = true, null=true) class meta: db_table=u'dish' def __unicode__ (self): return self.name
the line "print result.resto" prints name of resto.
print result.resto.name , print result.resto.area not print anything.
why that? need fields of resto?
search_indexes.py
import datetime haystack import indexes litchee.models import resto litchee.models import dish class restoindex(indexes.searchindex, indexes.indexable): text = indexes.charfield(document=true, use_template=true) name = indexes.charfield(model_attr='name') area = indexes.charfield(model_attr='area') def get_model(self): return resto def index_queryset(self, using=none): """used when entire index model updated.""" return self.get_model().objects.all() class dishindex(indexes.searchindex, indexes.indexable): text = indexes.charfield(document=true, use_template=true) name = indexes.charfield(model_attr='name') resto = indexes.charfield(model_attr='resto') def get_model(self): return dish def index_queryset(self, using=none): """used when entire index model updated.""" return self.get_model().objects.all() #add support shutdown
as mentioned in comment, you're not querying django models, you're querying search engine via haystack. isn't instance of model, . in search index dish model, resto
charfield, naturally that's in search results, rather reference model.
you should able access actual django model instance each result via object
attribute , there can follow foreign key.
note if you're new django, might want forget haystack until you've understood how actual model api works. may not need @ all.
Comments
Post a Comment