序列项0:预期的str实例,找到的字节

时间:2017-07-06 17:17:30

标签: python django

之前已经问过这个问题,但我没有得到错误的地方。基本上我有一个多对多的表。我正在执行复杂的查询,所以我写了这样的视图。像这样付费

def flightBooking(request):
    if request.method=='POST':
        form= SearchForm(request.POST)
        if not form.is_valid():
          return render(request, 'booking/search.html',{'form':form})
        else:
                form.is_valid()
                data=(form.cleaned_data['fromLocation'],form.cleaned_data['toLocation'])
                cursor=connection.cursor()
                cursor.execute("""select flight.name,scheduleDepart.depart_time,scheduleArrive.arrival_time.ai.name from 
                flight flight,airline ai,flight_schedule scheduleDepart inner join  
                flight_schedule scheduleArrive on scheduleDepart.flight_id=scheduleArrive.flight_id where scheduleArrive.location_id=%d and scheduleDepart.location_id=%d,
                 and scheduleArrive.flight_id=flight.id and flight.airline_code_id=ai.id """,[data])
                results=dictFetchAll(cursor)
                return render(results,'booking/searchResult.html')
                #return HttpResponse(results,'booking/searchResult.html')

def dictFetchAll(cursor):
    "return all rows with column name"
    columns=[col[0] for col in cursor.description]
    return [
            dict(zip(columns,row))
            for row in cursor.fetchall()
        ]

这里我的from和to location是整数字段 我有像这样的堆栈跟踪

  File "C:\Program Files (x86)\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
    response = get_response(request)
  File "C:\Program Files (x86)\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Program Files (x86)\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "E:\kepsla-workspace\WS\demoWS\flightBooking\booking\views.py", line 29, in flightBooking
    and scheduleArrive.flight_id=flight.id and flight.airline_code_id=ai.id """,[data])
  File "C:\Program Files (x86)\lib\site-packages\django\db\backends\utils.py", line 80, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Program Files (x86)\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\Program Files (x86)\lib\site-packages\django\db\backends\mysql\base.py", line 101, in execute
    return self.cursor.execute(query, args)
  File "C:\Program Files (x86)\lib\site-packages\MySQLdb\cursors.py", line 234, in execute
    args = tuple(map(db.literal, args))
  File "C:\Program Files (x86)\lib\site-packages\MySQLdb\connections.py", line 316, in literal
    s = self.escape(o, self.encoders)
  File "C:\Program Files (x86)\lib\site-packages\MySQLdb\converters.py", line 90, in quote_tuple
    return "(%s)" % (','.join(escape_sequence(t, d)))
TypeError: sequence item 0: expected str instance, bytes found
[06/Jul/2017 22:45:52] "POST /booking HTTP/1.1" 500 97454

所以有人可以告诉我哪里出错了吗?或者有没有其他方法来编写此查询??

1 个答案:

答案 0 :(得分:-1)

我有这样的解决方案

select flight.name,scheduleDepart.depart_time,scheduleArrive.arrival_time.ai.name from 
                flight flight,airline ai,flight_schedule scheduleDepart inner join  
                flight_schedule scheduleArrive on scheduleDepart.flight_id=scheduleArrive.flight_id where scheduleArrive.location_id=%d and scheduleDepart.location_id=%d,
                 and scheduleArrive.flight_id=flight.id and flight.airline_code_id=ai.id """.join(''.join(elems) for elems in data