Django:打印 QuerySet 时出错

Django: When print a QuerySet gives an error

我在下面的数据库中有一个 table(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

使用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是一个queryset/list,SavedSchedules 无法查找它的讲师=列表的对象。使用我上面的修改来获取单个讲师,或者使用此语法获取讲师来自列表的 SavedSchedules:

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

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