On Sep 25, 2011, at 7:45 PM, Russell Keith-Magee wrote: > On Sun, Sep 25, 2011 at 9:00 PM, David <david.pull...@gmail.com> wrote: >> I'm new to Django, gone through the django book and some >> documentation, but I can't find how to do this or if it's possible. >> >> I have a model that lists documents with title and edition. It also >> has a foreign key to an organization table. >> >> I'm trying to create a query form that first takes the name of the >> organization in one field, and they lets the user search by document >> title. The idea is to only show results in the document search that >> are from the organization. I'm not sure if this is something for the >> forms.py or if it's done in the view for the form. > > It sounds like you're getting a little tangled over what processing > should be handled where. > > A form retrieves inputs from the user. A query retrieves data from the > database. The view is a function that takes a request, from the user > and turns it into a response that can be rendered back to the user. > > So - for your purposes, you need a form to retrieve from the user the > organization they want to search, and the title of the document to > search for: > > class SearchForm(forms.Form): > organization = forms.ModelField(Organization) > search_term = forms.CharField(max_length=100) > > i.e., a form that lets you select an organization, and provide a block > of text for searching. > > Then, in your view, use the data from that form to issue a query: > > def my_view(request): > ... > form = SearchForm(data=request.GET) > if form.is_valid(): > organzation = form.cleaned_data['organization'] > search_term = form.cleaned_data]['search_term'] > results = Document.objects.filter(organization=organization, > title__icontains=search_term) > .... > > At this point, results will contain the list of documents that are > from the selected organization, and has a title that is a > case-insensitive partial match for the search term. If you want > different matching criteria, alter the __icontains query term. > > The view requires a little more fleshing out, tool. Obviously, you'll > need to render `results` into a HTTPResponse. I've also assumed that > the query is being issued as a GET query -- which is possible, because > a search should be idempotent, but you *could* issue the search as a > POST, in which case the view handling will be slightly different. If > you're using the same view to present the original form *and* present > the results, you'll need to handle that case; you'll also need to > handle the case where the user doesn't provide a search term. > > Filling in these extra bits is left as an exercise for the reader :-) > Hopefully with the form/query/view thing clarified, the rest will fall > into place. If it doesn't, feel free to ask. > > Yours, > Russ Magee %-) > > -- > 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. >
Russ, Thanks very much for helping to separate the processing. I'm trying to implement a setup using your suggestions. I've hit a snag on a detail: > class SearchForm(forms.Form): > organization = forms.ModelField(Organization) > search_term = forms.CharField(max_length=100) What's a ModelField? I was unable to find it in a search on docs.djangoproject.com. I found it referred to in an abstract way on a google search, but not as an actual attribute name. Thanks! David -- 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.