решила приспособить пример из django book с формой для поиска для фильтрации строк в таблице, все время сталкиваюсь с одной и той же проблемой, все работает, только если делать отбор по одному полю, то есьт если в query передавать только один атрибут для отбора, а как сделать, чтобы выбор можно было осуществлять по нескольким параметрам?
Увы, телепаты в отпуске :-)
lorien
Увы, телепаты в отпуске :-)
ну давайте с Вами вместе рассуждать на эту тему)))
смотрите
кусок из view:
def test_search(request):
        query = request.GET.get('q', '')
        if query:
                qset = (

                Q(creation_date=query)
                )
                results = Thread.objects.filter(qset).distinct()
        else:
                results = Thread.objects.select_related()
        all_threads = Thread.objects.select_related()
        return render_to_response("test.html", {
                 "results": results,
                  "query": query
                 })
и эту всю красоту должен обработать test.html

<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 thread in results %}
        <li>{{ thread|escape }}</l1>
      {% endfor %}
      </ul>
    {% else %}
      <p>No thread found</p>
    {% endif %}
  {% else %}
 {% for thread in results %}
        <li>{{ thread|escape }}</l1>
        {{thread.performer}} {{thread.customer}}
      {% endfor %}

  {% endif %}
вот. наша же цель сделать поиск по нескольком атрибутам
согласно примеру:
будет так
qset = (
Q(creation_time=query)|
Q(creation_date=query)
)

но если делать так выдает сообщение:
Warning at /testmy/
Incorrect time value: '2008-06-23' for column 'creation_time' at row 1

Last edited June 24, 2008, 5:16 p.m.

Это ужасно ) Попробуй заключить код в CODE тэг - вообще ничего непонятно )
lorien
Это ужасно ) Попробуй заключить код в CODE тэг - вообще ничего непонятно )
А можно перейти на ты?
собственно, твое пожелание выполнено
давай вместе подумаем как можно сделать вывод по двум полям
а то мне видимо самой это не одолеть
Ну, я так понимаю, что
Q(creation_time=query)
желает видеть в query строковое отображение времени, содержащее часы, минуты, секунды, а не только время.
оказалось все намного проще))
вот вью
def test_search(request):
        status = request.GET.get('q', '')
        date = request.GET.get('d', '')
        time = request.GET.get('q', '')
        date = request.GET.get('d', '')
        tit = request.GET.get('t', '')
        test =[]
        if date and status and tit:
                test = Thread.objects.select_related().filter(status__icontains=status).filter(title=tit).filter(creation_date=date)

        elif date and status:
                test = Thread.objects.select_related().filter(status__icontains=status).filter(creation_date=date)
        elif status and tit:
                test = Thread.objects.select_related().filter(status__icontains=status).filter(title=tit)
        elif date and tit:
                test = Thread.objects.select_related().filter(title=tit).filter(creation_date=date)
        elif date:
                test = Thread.objects.select_related().filter(creation_date=date)
        elif status:
                test = Thread.objects.select_related().filter(status__icontains=status)
        elif tit:
                test = Thread.objects.select_related().filter(title=tit)
        else:
                test = Thread.objects.select_related()
        results = test
        return render_to_response("test.html", {
                 "results": results,
                "tit": tit,
                  "date":date,
                "status": status
                 })
а вот и test.html
<form action="." method="GET">
    <label for="q">status: </label>
    <input type="text" name="q" value="{{ time|escape }}">
    <label for="d">date: </label>
    <input type="text" name="d" value="{{ date|escape }}">
    <label for="t">title: </label>
    <input type="text" name="t" value="{{ tit|escape }}">
    <input type="submit" value="Search">
  </form>

  {% if time or  date or tit %}
    <h2>Results for "{{ query|escape }}":</h2>

    {% if results %}
      {% for thread in results %}
        {{thread.id}} {{ thread|escape }}<br>
      {% endfor %}
    {% else %}
      <p>No thread found</p>
    {% endif %}
  {% else %}
 {% for thread in results %}
{{thread.id}} {{ thread|escape }}
        {{thread.performer}} {{thread.customer}}<br>
      {% endfor %}

  {% endif %}
так что с фильтрацией теперь разобралась)))

Last edited June 25, 2008, 10:19 a.m.