my view is below.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <title>Search{% if query %} Results{% endif %}</title>
</head>
<body>
  <h1>Search</h1>
  <form action="." method="GET">
    <label for="q">Search: </label>
    <input type="text" name="q" value="{{ query|escape }}">
    <input type="submit" value="Search">
  </form>

  {% if query %}
    <h2>Results for "{{ query|escape }}":</h2>

    {% if results %}
      <ul>
      {% for company in results %}
        <li>{{ company|escape }}</l1>
      {% endfor %}
      </ul>
    {% else %}
      <p>No companys found</p>
    {% endif %}
  {% endif %}
</body>
</html>



On Sep 27, 4:45 pm, "[EMAIL PROTECTED]"
<[EMAIL PROTECTED]> wrote:
> I have a company model which relates to a product model 1-> M.
>
> My goal is to search both models and return a result set set that will
> show the company name and product name that are results of the query.
> The implementation below only shows the company name.
>
> 1. Can I send back the product as well?
> 2. Should I call a function from the template to retrieve the product?
>
> any help appreciated!
>
> my models are:
> class Product(models.Model):
>   co       = models.ForeignKey(Company)
>   key      = models.ForeignKey(Keyword)
>   name     = models.CharField(max_length=100)
>   pub_date = models.DateTimeField(default=datetime.datetime.now())
>
>   def __unicode__(self):
>     return self.name
>
> class Product(models.Model):
>   co       = models.ForeignKey(Company)
>   key      = models.ForeignKey(Keyword)
>   name     = models.CharField(max_length=100)
>   pub_date = models.DateTimeField(default=datetime.datetime.now())
>
>   def __unicode__(self):
>     return self.name
>
> My view is...
>
> from django.db.models import Q
> from django.shortcuts import render_to_response
> from models import Company
>
> def search(request):
>     query = request.GET.get('q', '')
>     if query:
>         qset = (
>             Q(name__icontains=query) |
>             Q(url__icontains=query) |
>             Q(product__name__icontains=query) |
>             Q(product__key__name__icontains=query)
>         )
>         results = Company.objects.filter(qset).distinct()
>     else:
>         results = []
>     return render_to_response("foods/search.html", {
>         "results": results,
>         "query": query
>     })
>
> My template is...
> from django.db.models import Q
> from django.shortcuts import render_to_response
> from models import Company
>
> def search(request):
>     query = request.GET.get('q', '')
>     if query:
>         qset = (
>             Q(name__icontains=query) |
>             Q(url__icontains=query) |
>             Q(product__name__icontains=query) |
>             Q(product__key__name__icontains=query)
>         )
>         results = Company.objects.filter(qset).distinct()
>     else:
>         results = []
>     return render_to_response("foods/search.html", {
>         "results": results,
>         "query": query
>     })
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to