【发布时间】:2020-08-14 20:13:37
【问题描述】:
我在数据库下面有一个表(SavedSchedules)。 hall_n_time 和 schedule 列将两个 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
【问题讨论】:
-
您能解释一下您要达到的目标吗?您是否正在尝试获取当前登录用户的“SavedSchedules”对象列表?
-
是的。 @MihaiChelaru
-
请展示你的模特
标签: python django python-3.x django-queryset