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 %}
<div id="formWrap">
  <form id="search_form" method="get" action=".">
    <table>
      <tr>
        <td><label for="id_sdo">SDO</label></td>
        <td>{{ form.sdo }}</td>
        <td><label for="id_document_search_term">Document Number</label></td>
        <td>{{ form.document_search_term }}</td>
      </tr>
    </table>
    <input type="submit" value="search" />
  </form>
  <div id="search_results">
    {% if show_results %}
    {% include "document_list.html" %}
    {% endif %}
  </div>
  {% endblock %}
</div>

#contents of document_list.html
{% if documents %}
<ul class="documents">
<table>
  {% for document in documents %}
  <tr>
    <td>{{ document.number }}</td><td>{{ document.title }}</td><td>{{ 
document.edition }}</td>
  </tr>
  {% endfor %}
</table>
</ul>
{% 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.

Reply via email to