Since a Company could have multiple Products, it's not really possible
to show the product as well with the way you do the querying.
I would instead do a search on the Product model and then simply
iterate over the resulting products displaying the company for each of
them, too:
def search(request):
q = request.GET.get('q', '')
if q:
qset = (
Q(co__name__icontains=q) |
Q(co__url__icontains=q) |
Q(name__icontains=q) |
Q(key__name__icontains=q)
)
results = Product.objects.filter(qset).distinct()
else:
results = []
return render_to_response(...)
In the template:
{% for product in results %}
<li>Product: {{ product }}, company: {{ product.co }}</li>
{% endfor %}
As a sidenote, I'd use readable column names such as "company" instead
of "co". If you need to map to a legacy database which has "co_id" as
the name of the column, simply do:
company = models.ForeignKey(Company, dbcolumn='co_id')
Erik
On 29.09.2008, at 1:29, Tom MacKenzie wrote:
> If anyone could have a look it would be much appreciated.
>
> -Tom
>
> On Sat, Sep 27, 2008 at 6:40 PM, Tom MacKenzie <[EMAIL PROTECTED]
> > wrote:
> http://django.pastebin.com/d1c579dbf
>
> Thanks!
>
>
> On Sat, Sep 27, 2008 at 5:48 PM, Ovnicraft <[EMAIL PROTECTED]>
> wrote:
> is posible use pastebin? http://django.pastebin.com/
>
> 2008/9/27 [EMAIL PROTECTED] <[EMAIL PROTECTED]>
>
> 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
> > })
>
>
>
>
> --
> [b]question = (to) ? be : !be; .[/b]
>
>
>
>
>
> >
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---