I'm trying to figure out the best way to create basic search forms in
Django. Currently I'm using newforms and have a simple view that takes
the parameters, calls the appropriate filters and returns the results.

--------------------------------------------------------------------------------
# newform
class ProjectSearchForm(forms.Form):
    p_type = forms.ModelChoiceField(queryset =
ProjectType.objects.all(), required=False)
    p_stage = forms.ModelChoiceField(queryset =
ProjectStage.objects.all(), required=False)
    p_status = forms.ModelChoiceField(queryset =
ProjectStatus.objects.all(), required=False)


# view
def project_search(request):
    p_type = request.GET.get('p_type', None)
    p_stage = request.GET.get('p_stage', None)
    p_status = request.GET.get('p_status', None)

    data = {'p_type':p_type,
            'p_stage':p_stage,
            'p_status':p_status,
            }
    search_form = ProjectSearchForm(data)
    if search_form.is_valid():
        results = Project.objects.all.(),order('name')
        if search_form.data['p_type']:
            results =
results.filter(p_type=search_form.data['p_type'])
        if search_form.data['p_stage']:
            results =
results.filter(p_type=search_form.data['p_stage'])
        if search_form.data['p_status']:
            results =
results.filter(p_type=search_form.data['p_status'])
    else
        results = []
    context = Context()
    context.update({'search_form': search_form})
    context.update({'results': results})
    return render_to_response('projects/projects_list.html',
RequestContext(request, context))

--------------------------------------------------------------------------------

However, it seems like I should be able to abstract things so that I
the actual field only show up in the form declaration.
  - I don't have to set each field to send into the form
  - I don't have to set a filter for each field


Something more like this:
--------------------------------------------------------------------------------
#view
def project_search(request):
    #create search form with current request variables or using intial
values for form?
    search_form = ProjectSearch(request.GET)
    if search_form.is_valid():
        #QuerySet filter created by search form?
        results = Project.objects.filter(search_form.filter)
    else:
        results = []
    context = Context()
    context.update({'search_form': search_form})
    context.update({'results': results})
    return render_to_response('projects/projects_list.html',
RequestContext(request, context))
--------------------------------------------------------------------------------

I've been looking around a bit at generic views, but it didn't seem to
be quite what I am looking for. Is this type of generic search form
currently possible?

Any help would be appreciated. Thanks!

Matt

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

Reply via email to