I have a Project model with a foreign key to institution.  I want to
do a Q(institution_icontains)=queryr
on the institution field in the institution table, but the filter
needs to go through the Projects model.
I need all institutions associated with a Project.


class Project(models.Model):
    restitle = models.TextField(null=True, blank=True)
    institution = models.ForeignKey(Institution, null=True,
blank=True)
    contact = models.ForeignKey(Contact, null=True, blank=True)
    def __unicode__(self):
        return self.restitle
    class Meta:
        ordering = ["restitle"]

class Institution(models.Model):
    institution = models.TextField(null=True, blank=True)
    def __unicode__(self):
        return self.institution
    class Meta:
        ordering = ["institution"]


def projects(request):
    queryr = request.GET.get('qr', '')
    if queryr:
        qset = (
           Q(projecttitle__icontains=queryr)
        )
        resultsr = Project.objects.filter(qset).distinct() # original
        qset2 = (
           Q(institution__icontains=queryr)
        )
        resultsi = Institution.objects.filter(qset2).distinct()
    else:
        resultsr = []
        resultsi = []
    return render_to_response("search/search.html", {
        "queryr": queryr,
        "resultsr": resultsr,
        "resultsi": resultsi,

This statement gives me an error, because institution is not in
Project, but the institution_id.
qset = (
           Q(projecttitle__icontains=queryr) |
           Q(institution__icontains=queryr)
        )


This statement selects results from both models (projects and
institutions), but institution is
independent of the Projects.

        qset = (
        Q(projecttitle__icontains=queryr)
        )
        resultsr = Project.objects.filter(qset).distinct() # original
        qset2 = (
        Q(institution__icontains=queryr)
        )
        resultsi = Institution.objects.filter(qset2).distinct()

Can anyone please help me with the syntax?


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to