Re: How to make a query form depend on a field?

2011-09-27 Thread David G. Pullman

On Sep 26, 2011, at 5:06 PM, Doug Ballance wrote:

> There are also a few third-party apps that can assist you with the
> form->query process such as this one:
> 
> https://github.com/alex/django-filter
> 
> -- 
> 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.
> 

Thanks, Doug.  May be very useful if they start asking for more varied kinds of 
queries.  So far I've only been given the first one, but I know more are coming.

--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.



Re: How to make a query form depend on a field?

2011-09-26 Thread Russell Keith-Magee
On Mon, Sep 26, 2011 at 11:59 PM, Tom Evans  wrote:
> On Mon, Sep 26, 2011 at 4:26 PM, David G. Pullman
>  wrote:
>> 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.
>>
>
> 100% certain that was just a brain fade on Russell's part, it should
> be forms.ModelChoiceField
>
> https://docs.djangoproject.com/en/1.3/ref/forms/fields/#modelchoicefield

Gah - yes, you've correctly diagnosed my degenerative brain disorder... :-)

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.



Re: How to make a query form depend on a field?

2011-09-26 Thread Doug Ballance
There are also a few third-party apps that can assist you with the
form->query process such as this one:

https://github.com/alex/django-filter

-- 
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.



Re: How to make a query form depend on a field?

2011-09-26 Thread David G. Pullman

On Sep 26, 2011, at 11:59 AM, Tom Evans wrote:

> 100% certain that was just a brain fade on Russell's part, it should
> be forms.ModelChoiceField
> 
> https://docs.djangoproject.com/en/1.3/ref/forms/fields/#modelchoicefield
> 
> Cheers
> 
> Tom
> 
> -- 
> 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 and Tom,

Thanks to both of you.  It's  working.  Here's what I did (Sdo is the 
Organization):

# snip of models.py
class Sdo(models.Model):
name = models.CharField(max_length=255, unique=True)
background = models.TextField(blank=True, null=True)
address = models.CharField(max_length=255, blank=True, null=True)
phone = models.CharField(max_length=255, blank=True, null=True)
url = models.CharField(max_length=255, blank=True, null=True)

def __unicode__(self):
return self.name

class Document(models.Model):
number = models.CharField(max_length=255)
title = models.CharField(max_length=255)
edition = models.CharField(max_length=255)
scope = models.CharField(max_length=255, blank=True, null=True)
sdo = models.ForeignKey(Sdo)

class Meta:
unique_together = (('number', 'title', 'edition'),)

def __unicode__(self):
return u'%s %s %s' % (self.number, self.title, self.edition)

# snip from forms.py
class SearchForm(forms.Form):
sdo = forms.ModelChoiceField(queryset=Sdo.objects.all())
document_search_term = forms.CharField(max_length=100)

# snip from views.py
def search_form(request):
form = SearchForm(data=request.GET)
if form.is_valid():
sdo = form.cleaned_data['sdo']
document_search_term = form.cleaned_data['document_search_term']
documents = Document.objects.filter(sdo=sdo, 
number__icontains=document_search_term)
results = RequestContext(request, {
'form': form,
'documents': documents,
'show_results': True
})
return render_to_response('search_form.html', results)
else:
return render_to_response('search_form.html', locals())

# contents of search_form.html
{% extends "base.html" %}
{% block external %}
{% endblock %}
{% block title %}SDO Search{% endblock %}
{% block head %}SDO Search{% endblock %}
{% block content %}

  

  
SDO
{{ form.sdo }}
Document Number
{{ form.document_search_term }}
  


  
  
{% if show_results %}
{% include "document_list.html" %}
{% endif %}
  
  {% endblock %}


#contents of document_list.html
{% if documents %}


  {% for document in documents %}
  
{{ document.number }}{{ document.title }}{{ 
document.edition }}
  
  {% endfor %}


{% endif %}

There is a simple url.py entry for /search_form/.  It provides a list of the 
documents that have the sdo_id that matches the id field in Sdo for the 
selected value of the Sdo name in the sdo field of the request.

I know some of this is goofy as I'm working from tuts and the examples in the 
docs and such, but I would like to say that I'm impressed how relatively easy 
it is to get something working in Django.  Especially with such a helpful users 
mailing list!

--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.



Re: How to make a query form depend on a field?

2011-09-26 Thread Tom Evans
On Mon, Sep 26, 2011 at 4:26 PM, David G. Pullman
 wrote:
> 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.
>

100% certain that was just a brain fade on Russell's part, it should
be forms.ModelChoiceField

https://docs.djangoproject.com/en/1.3/ref/forms/fields/#modelchoicefield

Cheers

Tom

-- 
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.



Re: How to make a query form depend on a field?

2011-09-26 Thread David G. Pullman

On Sep 25, 2011, at 7:45 PM, Russell Keith-Magee wrote:

> On Sun, Sep 25, 2011 at 9:00 PM, David  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.



Re: How to make a query form depend on a field?

2011-09-25 Thread Russell Keith-Magee
On Sun, Sep 25, 2011 at 9:00 PM, David  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.