【发布时间】:2019-09-24 17:11:53
【问题描述】:
使用 Django 基本用户模型。使用基本模型 pk 时出现重复错误。不知道为什么我的应用程序似乎无法确定哪个用户正在发表评论。
def view_api(request):
user = request.user
if request.method == 'POST':
if request.user.is_active:
price, created = Model.objects.get_or_create(
user=User.objects.filter(pk=user.pk),
anonymous_user=False,
comments=request.POST.get('comments')
)
错误:
The QuerySet value for an exact lookup must be limited to one result using slicing.
追溯:
File "/Users/~/query.py" in get_or_create
538. return self.get(**kwargs), False
File "/Users/~/query.py" in get
402. num = len(clone)
File "/Users/~/query.py" in __len__
256. self._fetch_all()
File "/Users/~/query.py" in _fetch_all
1242. self._result_cache = list(self._iterable_class(self))
File "/Users/~/query.py" in __iter__
55. results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/Users/~/compiler.py" in execute_sql
1087. sql, params = self.as_sql()
File "/Users/~/compiler.py" in as_sql
489. where, w_params = self.compile(self.where) if self.where is not None else ("", [])
File "/Users/~/compiler.py" in compile
405. sql, params = node.as_sql(self, self.connection)
File "/Users/~/where.py" in as_sql
81. sql, params = compiler.compile(child)
File "/Users/~/compiler.py" in compile
405. sql, params = node.as_sql(self, self.connection)
File "/Users/~/related_lookups.py" in as_sql
130. return super().as_sql(compiler, connection)
File "/Users/~/lookups.py" in as_sql
163. rhs_sql, rhs_params = self.process_rhs(compiler, connection)
File "/Users/~/" in process_rhs
257. 'The QuerySet value for an exact lookup must be limited to '
Exception Type: ValueError at /api/voting/
Exception Value: The QuerySet value for an exact lookup must be limited to one result using slicing.
【问题讨论】:
标签: django primary-key django-users