Django:打印查询集时出现错误

问题描述 投票:-2回答:1

我在数据库中下面有一个表(SavedSchedules)。 hall_n_timeschedule列将两个python列表存储为字符串。

+----+---------------+-------------+----------+
| id |   lecturer    | hall_n_time | schedule |
+----+---------------+-------------+----------+
|... |      ...      |     ...     |    ...   |
+----+---------------+-------------+----------+

我在views.py中有以下脚本:

lec_name = User.objects.filter(username=request.session['logged_username']).values_list('lecturer_name', flat=True)
print(lec_name)

这将提供输出:

<QuerySet ['A. B. C. Watson']>

但预期的输出:

A. B. C. Watson

然后我尝试使用以下脚本:

lec_name = User.objects.filter(username=request.session['logged_username']).values_list('lecturer_name', flat=True)
schedule_data = SavedSchedules.objects.filter(lecturer=lec_name)
print(schedule_data)

当我执行它时,它在错误下面给出:

Traceback (most recent call last):
  File "C:\Users\BhathiyaTK\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\BhathiyaTK\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\BhathiyaTK\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "D:\Web Projects\CIS_ALTG\altg\altg_app\views.py", line 497, in profile
    print(schedule_data)
  File "C:\Users\BhathiyaTK\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py", line 252, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "C:\Users\BhathiyaTK\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py", line 276, in __iter__
    self._fetch_all()
  File "C:\Users\BhathiyaTK\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py", line 1261, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Users\BhathiyaTK\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py", line 57, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "C:\Users\BhathiyaTK\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\compiler.py", line 1124, in execute_sql
    sql, params = self.as_sql()
  File "C:\Users\BhathiyaTK\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\compiler.py", line 498, in as_sql
    where, w_params = self.compile(self.where) if self.where is not None else ("", [])
  File "C:\Users\BhathiyaTK\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\compiler.py", line 415, in compile
    sql, params = node.as_sql(self, self.connection)
  File "C:\Users\BhathiyaTK\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\where.py", line 81, in as_sql
    sql, params = compiler.compile(child)
  File "C:\Users\BhathiyaTK\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\compiler.py", line 415, in compile
    sql, params = node.as_sql(self, self.connection)
  File "C:\Users\BhathiyaTK\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\lookups.py", line 177, in as_sql
    rhs_sql, rhs_params = self.process_rhs(compiler, connection)
  File "C:\Users\BhathiyaTK\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\lookups.py", line 270, in process_rhs
    'The QuerySet value for an exact lookup must be limited to '
ValueError: The QuerySet value for an exact lookup must be limited to one result using slicing.
[30/Apr/2020 17:26:37] "GET /profile/ HTTP/1.1" 500 122259

我是Django的新手,我不知道为什么会出现这种错误。有人可以解释为什么会发生这种情况以及如何解决这些问题吗?我感谢您的帮助。我正在使用Python 3.7.4,Django 3.0.1

python django python-3.x django-queryset
1个回答
0
投票

使用values_list仍会给您一个查询集,但其中包含一个列表。您可以使用以下方法将值作为纯列表获取:

lec_name = list(User.objects.filter(username=request.session['logged_username']).values_list('lecturer_name', flat=True))[0]
print(lec_name)

这将获得列表中的第一个元素,应该是您的讲师姓名。

问题所在

schedule_data = SavedSchedules.objects.filter(lecturer=lec_name)
print(schedule_data)

是您当前的形式,lec_name是一个查询集/列表,并且SavedSchedules无法在其讲师=列表的情况下查找对象。请使用我上面的修改来获得一名讲师,或者使用以下语法来获取SavedSchedules,其中讲师位于列表中:

schedule_data = SavedSchedules.objects.filter(lecturer__in=lec_list)

希望这些建议有帮助,编码愉快!

© www.soinside.com 2019 - 2024. All rights reserved.