【问题标题】:How to retrieve objects that are not created by the user? [duplicate]如何检索不是由用户创建的对象? [复制]
【发布时间】:2019-10-03 12:20:24
【问题描述】:

这个问题可能很奇怪,但我想检索除用户登录创建的对象之外的对象

好吧,我在我的视图中尝试了这段代码

def getobjects(request):
    products=Products.objects.filter(user!= request.user)

但不起作用。

在模型中

class Products(models.Model):
    name=models.Charfield()
    user=models.ForeignKey(User)

我需要有关正确查询功能的帮助来处理此问题。提前致谢

【问题讨论】:

标签: python django django-models django-orm


【解决方案1】:

您可以排除请求用户

products=Products.objects.exclude(user = request.user)

【讨论】:

  • 我得到这个错误,“name 'user' is not defined”
  • 试试这个.exclude(user_id = request.user.pk) 并确保你的请求有用户对象。
  • 举个例子?我的意思是评论或问题的解决方案?
  • 来自您的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-05
  • 2014-02-08
  • 2019-04-13
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 2021-10-01
相关资源
最近更新 更多