【问题标题】:Django: When print a QuerySet gives an errorDjango:打印查询集时出错
【发布时间】:2020-08-14 20:13:37
【问题描述】:

我在数据库下面有一个表(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】:

使用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)

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

【讨论】:

    猜你喜欢
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多