【问题标题】:Queryset question about base user model Django关于基本用户模型 Django 的查询集问题
【发布时间】: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


    【解决方案1】:

    QuerySet 是对象的集合。但是将Users 的集合(即使它只包含一个用户)传递给user 字段没有多大意义。

    你不需要在这里使用QuerySet:你可以直接使用request.user,比如:

    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
                    anonymous_user=False,
                    comments=request.POST.get('comments')
                )
        # …

    【讨论】:

    • 这是我的第一次尝试,它绝对行不通。得到同样的错误:精确查找的 QuerySet 值必须使用切片限制为一个结果。
    • @user10421193:我非常确信错误不在这一行。它很可能位于其他地方。您可以使用回溯编辑您的问题吗?
    • 这是一个网站模态表单。由于此错误,我无法提交表单。这是来自 Inspect 元素的回溯。我不断收到的另一个错误是:无法将关键字“用户”解析为字段。选择是: 。因此,我尝试了另一件事。
    • @user10421193:但它没有显示在视图的哪一行会产生错误。
    • 是的,因为这个错误是从 api 直接传到 Safari 浏览器的。这是我得到的另一个错误:无法分配“]>”:“Voting.user”必须是“用户”实例。
    猜你喜欢
    • 2011-06-03
    • 2016-11-22
    • 2020-04-20
    • 2021-12-29
    • 2017-07-30
    • 2018-10-20
    • 2020-08-02
    • 2017-10-18
    • 1970-01-01
    相关资源
    最近更新 更多