为什么过滤QuerySet对用户与超级用户的行为有所不同?

时间:2022-10-21 06:36:39

When logged in as my superuser, 'admin', I'm able to filter a QuerySet by any field. However when I am logged in as a regular user I'm only able to filter by the field 'user'. Trying to filter by any other field returns an empty list.

以超级用户'admin'身份登录时,我可以按任何字段过滤QuerySet。但是,当我以普通用户身份登录时,我只能通过“用户”字段进行过滤。尝试按任何其他字段筛选返回空列表。

class Question(models.Model):

    user = models.ForeignKey(User, default=1)
    verb = models.ForeignKey(Verb)
    voice = models.ForeignKey(Voice)
    ...

This works fine:

这很好用:

def next_question(request):
    user = request.user
    q = Question.objects.filter(user=user)

This works fine when logged in as a superuser but fails as a regular user (myverb is an instance of my Verb model):

以超级用户身份登录时可以正常工作,但作为常规用户失败(myverb是我的Verb模型的一个实例):

def next_question(request):
    user = request.user
    q = Question.objects.filter(user=user, verb=myverb)

I'm new to Django (though not to Python) so am I missing some kind of permission that I need to set for regular users which would fix this behaviour?

我是Django的新手(虽然不是Python)所以我错过了一些我需要为普通用户设置的权限,这会修复这种行为吗?

Edited to add images showing the items in the database

编辑以添加显示数据库中项目的图像

Example question for superuser, 'admin':

超级用户的示例问题'admin':

为什么过滤QuerySet对用户与超级用户的行为有所不同?

Example question for user 'jamie':

用户'jamie'的示例问题:

为什么过滤QuerySet对用户与超级用户的行为有所不同?

2 个解决方案

#1


There is no difference between superuser and regular user in this ORM query.

在此ORM查询中,超级用户和普通用户之间没有区别。

You simply don't have any Question instances with both myverb verb field and request.user user field.

您只是没有myverb verb字段和request.user用户字段的任何Question实例。

#2


There is no difference. The super user is also a normal user.

没有区别。超级用户也是普通用户。

Depending on what the value of the user variable is set to at the time (I assume it's the currently logged in user) You could interpret your query as this: all questions where the related user is the currently logged in user.

取决于当时设置的用户变量的值(我假设它是当前登录的用户)您可以将您的查询解释为:相关用户是当前登录用户的所有问题。

Therefore if you see no results when you are logged in as someone else it is because you really don't have any questions you are associated with.

因此,如果您在以其他人身份登录时看不到任何结果,那是因为您确实没有任何与之相关的问题。

If you are sure that you should have results it may be that your user variable is not set to what you think it is. Make sure it's value is correct.

如果您确定应该有结果,则可能是您的用户变量未设置为您认为的结果。确保它的值是正确的。

Ie user = request.user

即user = request.user

#1


There is no difference between superuser and regular user in this ORM query.

在此ORM查询中,超级用户和普通用户之间没有区别。

You simply don't have any Question instances with both myverb verb field and request.user user field.

您只是没有myverb verb字段和request.user用户字段的任何Question实例。

#2


There is no difference. The super user is also a normal user.

没有区别。超级用户也是普通用户。

Depending on what the value of the user variable is set to at the time (I assume it's the currently logged in user) You could interpret your query as this: all questions where the related user is the currently logged in user.

取决于当时设置的用户变量的值(我假设它是当前登录的用户)您可以将您的查询解释为:相关用户是当前登录用户的所有问题。

Therefore if you see no results when you are logged in as someone else it is because you really don't have any questions you are associated with.

因此,如果您在以其他人身份登录时看不到任何结果,那是因为您确实没有任何与之相关的问题。

If you are sure that you should have results it may be that your user variable is not set to what you think it is. Make sure it's value is correct.

如果您确定应该有结果,则可能是您的用户变量未设置为您认为的结果。确保它的值是正确的。

Ie user = request.user

即user = request.user