'function'对象没有属性'对象'Django 1.8搜索模型

时间:2015-06-30 02:45:26

标签: python django

我收到了这个错误:

Exception Type: AttributeError
Exception Value:    
'function' object has no attribute 'objects'
web/views.py in consultarE, line 153

来自:

views.py

def consultarE(request):
    query = request.GET.get('q', '')
    if query:
        qset = (
            Q(nombres__icontains=query) |
            Q(apellidos__icontains=query) |
            Q(telefono__icontains=query)
            # Q(cargo__icontains=query)
        )
        results = Empleado.objects.filter(qset).distinct()
    else:
        results = []
    return render_to_response("web/consultarEmpleado.html", {
        "results": results,
        "query": query        
    })

urls.py

url(r'^consultarEmpleado/$','consultarE',name='consultarEmpleado'),

consultarEmpleado.html

{% block content%}
  <h1>Ingrese su búsqueda</h1>
  <form class="navbar-form" role="search" action="." method="GET"> 
    {{form|crispy}}
    <label for="q">Buscar: </label>
    <div class="input-group">
      <input type="text" class="form-control" placeholder="Search" name="q" value="{{ query|escape }}">  
      <div class="input-group-btn">
        <button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
      </div>
    </div>
  </form>

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

    {% if results %}
      <ul>
        {% for empleado in results %}
          <li>{{ empleado|escape }}</l1>
        {% endfor %}
      </ul>
    {% else %}
      <p>No se encontró empleado</p>
    {% endif %}

  {% endif %}

{%endblock%}

1 个答案:

答案 0 :(得分:0)

检查是否有一个名为Empleado的函数覆盖对Empleado模型的引用。

def Empleado(...):
    ....

调整功能名称,使其不与型号名称冲突。