python - Django query in detailview -
i have detailview returns list of related objects (m2m through). works fine!
but need search objects' names , returning objects instead of related ones.
how can approach this?
thanks.
#models.py class candidate(models.model): user = models.onetoonefield(user, primary_key=true) birth = models.charfield(max_length=50) ... class job(models.model): candidate = models.manytomanyfield('candidate', through='candidatetojob') title = models.charfield(max_length=500) ... class candidatetojob(models.model): job = models.foreignkey(job, related_name='applied_to') candidate = models.foreignkey(candidate, related_name='from_user') status_choices = ( ('1', 'not approved'), ('2', 'approved'), ('3', 'hired') ) status = models.charfield(max_length=2, choices=status_choices)
my search query
#views.py def get_query(query_string, search_fields): query = none # query search every search term terms = normalize_query(query_string) term in terms: or_query = none # query search given term in each field field_name in search_fields: q = q(**{"%s__icontains" % field_name: term}) if or_query none: or_query = q else: or_query = or_query | q if query none: query = or_query else: query = query & or_query homecoming query def searchcandidate(request, *args, **kwargs): query_string = '' found_entries = none if ('q' in request.get) , request.get['q'].strip(): query_string = request.get['q'] entry_query = get_query(query_string, ['candidate__user__first_name', 'candidate__user__last_name']) found_entries = candidatetojob.objects.filter(entry_query).order_by('-candidate') homecoming render_to_response('dashboard/candidates_results.html', { 'query_string': query_string, 'found_entries': found_entries }, context_instance=requestcontext(request) )
the view list of objects(candidates)
class screening(generic.detailview): model = job template_name = 'dashboard/screening.html' def get_context_data(self, **kwargs): context = super(screening, self).get_context_data(**kwargs) context['candidate_list'] = self.object.applied_to.all().order_by('candidate') homecoming context
my urls
#urls.py url(r'^dashboard/job/(?p<pk>\d+)/screening/$', views.screening.as_view(), name='screening'), url(r'^dashboard/job/(?p<pk>\d+)/screening/results/$', 'companies.views.searchcandidate', name='searchcandidate'),
and templates
#html screening(detailview) in works <form class="" method="get" action="{% url 'searchcandidate' job.pk %}"> <div class="input-group"> <input name="q" id="id_q" type="text" class="form-control" placeholder="buscar candidatos por nome" /> <span class="input-group-btn"> <button class="btn btn-orange" type="submit">buscar</button> </span> </div> </form> {% candidate in candidate_list %} {{ candidate }} {% endfor %}
the 1 in returning objects instead of related ones
#html {% if found_entries %} {% candidate in found_entries %} {{ candidate }} {% endfor %} {% endif %}
i got working. , how made it.
i changed def searchcandidate for:
#views.py class searchcandidate(generic.detailview): model = job template_name = 'dashboard/screening-results.html' def get_context_data(self, **kwargs): context = super(searchcandidate, self).get_context_data(**kwargs) context['candidate_list'] = self.object.applied_to.filter( get_query(self.request.get['q'], ['candidate__user__first_name', 'candidate__user__last_name']) ).order_by('candidate') homecoming context
python django many-to-many search-engine m2m
No comments:
Post a Comment