【问题标题】:How do I filter a model object and update a view in django?如何过滤模型对象并更新 django 中的视图?
【发布时间】:2016-03-10 02:56:45
【问题描述】:

我在 django 中有一个带有 html 表的模板。视图顶部是一个用于输入的小表单。比如:

 {% csrf_token %}
<form method="post" name="some_name" action="/myApp/">
  search button here
  ...
</form>
<table>
 loop through data and make table here
</table>

当我访问网页时,我将数据初始化到表中。我有一个超过 2500 万行的后端 sqlite 数据库。我想使用表单中的用户输入来过滤数据。我已经尝试从表单中获取数据,但是当我尝试应用 objects.all.filter(some_condition) 时,我没有看到表更新。我究竟做错了什么?或者有其他人想出解决此类问题的方法吗?我知道这应该很简单,但我有时间弄清楚了。谢谢!

更新:

views.py

def showTable(request):
   if request.method == 'POST':

       #I have a table of aircraft entries.The EntriesTable has a ForeignKey
       #referencing an AircraftTable that has the numbers of aircraft
       #some_name will reference the number of the aircraft

        aircraft = request.POST.get('some_name', None)
        query_results = EntriesTable.objects.all().filter(aircraft__exact = filterable)
        template=loader.get_template('myApp/showTable.html')
        context=RequestContext(request, {'query_results': query_results,})
        return HttpResponse(template.render(context))
   else:
       query_results = EntriesTable.objects.all().filter(start_time__range(start, stop)) #Assume that I have correctly filtered on start/stop times.  I am able to render that so far without issue.
       context= RequestContext(request, {'query_results': query_results,})
       return HttpResponse(template.render(context))

【问题讨论】:

  • 你的视图函数是什么样的?
  • 用views.py更新

标签: python django filtering


【解决方案1】:
 query_results = EntriesTable.objects.filter(aircraft__exact = filterable)

你的观点有错误,语法应该是我:

NameOfModelClass.objects.filter(条件)

我相信这会解决问题

【讨论】:

    猜你喜欢
    • 2020-11-01
    • 2017-02-18
    • 2017-08-08
    • 1970-01-01
    • 2013-03-16
    • 2023-03-31
    • 2016-12-18
    • 2022-10-14
    相关资源
    最近更新 更多