Hi Ronaldo,

This list is for the discussion of development *on the Django project 
itself*,
not for discussion of usage questions when developing Django sites.

You want the django-users mailing list, 
here: https://groups.google.com/forum/#!forum/django-users

All the best,

  Tom

On Wednesday, 29 October 2014 16:20:02 UTC, Ronaldo Bahia wrote:
>
> *This question is also in 
> http://stackoverflow.com/questions/26635406/django-query-in-detailview
>
> I have DetailVIew wich returns a list of related objects (m2m throught). 
> It works just fine!
>
> But I need to search for objects'names and it is returning all objects 
> instead of only the related ones.
>
> How can I approach this?
>
> Thanks.
>
> #models.pyclass 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 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)
>         )
>
> The view with the 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')
>         return 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 the templates
>
> #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 cla
>
> ...

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/02785977-14fc-403e-81d4-d784bf497147%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to