2014-10-29 102 views
1

我有一個DetailVIew,它返回一個相關對象(m2m到)的列表。 它工作得很好!Django詳細查詢

但我需要搜索對象的名稱,它將返回所有對象,而不是僅返回相關對象。

我該如何解決這個問題?

謝謝。

#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) 

我的搜索查詢

#views.py 
def get_query(query_string, search_fields): 
    query = None # Query to search for every search term 
    terms = normalize_query(query_string) 
    for term in terms: 
     or_query = None # Query to search for a given term in each field 
     for field_name in search_fields: 
      q = Q(**{"%s__icontains" % field_name: term}) 
      if or_query is None: 
       or_query = q 
      else: 
       or_query = or_query | q 
     if query is None: 
      query = or_query 
     else: 
      query = query & or_query 
    return query 

def searchcandidate(request, *args, **kwargs): 
    query_string = '' 
    found_entries = None 
    if ('q' in request.GET) and 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') 

    return render_to_response('dashboard/candidates_results.html', 
      { 'query_string': query_string, 'found_entries': found_entries }, 
      context_instance=RequestContext(request) 
     ) 

與對象的列表視圖(候選人)

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') 
     return context 

我的網址

#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'), 

而且模板

#html for Screening(DetailView) in which works good 
<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> 

{% for candidate in candidate_list %} 
    {{ candidate }} 
{% endfor %} 

的一個,其中返回所有對象,而不是隻有相關的那些

#html 
{% if found_entries %} 
    {% for candidate in found_entries %} 
      {{ candidate }} 
    {% endfor %} 
{% endif %} 

回答

1

我終於得到它的工作。這就是我做到的。

我改變了高清searchcandidate爲:

#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') 

     return context