【问题标题】:Django tastypie: User sees no dataDjango 美味派:用户看不到任何数据
【发布时间】:2019-02-19 13:43:01
【问题描述】:

我想向我的 API 添加身份验证,因此只有授权的人才能看到数据。

我添加到我的资源类:

authentication = BasicAuthentication()
authorization = DjangoAuthorization()

然后我使用 Django admin 添加了一个新用户。他被列为活跃和工作人员。未授予其他权限。

现在我尝试资源 URL,它要求提供凭据。

当我使用新用户凭据时,我什么也得不到:

{"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 0}, "objects": []}

没有对象,什么都没有。如果我以 root 身份登录,我会看到所有数据。

如何将内容分配给用户,以便它可以查看资源?

【问题讨论】:

    标签: django tastypie


    【解决方案1】:

    首先,stuff status 仅指定用户是否可以登录此管理站点。 你应该看到 What's the difference between staff, admin, superuser in django?

    其次,Tastypie 的 DjangoAuthorization 检查用户在资源模型上授予他们的权限(通过 django.contrib.auth.models.Permission)。 https://django-tastypie.readthedocs.io/en/latest/authorization.html#djangoauthorization

    很明显,root可以看到所有数据的原因是root是superuser。因此,你可以这样做:

    • 授予用户超级用户;

    • 授予用户当前resource_model的读取权限(可以更改..);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-25
      • 2013-03-28
      • 1970-01-01
      • 2015-02-17
      • 1970-01-01
      • 2014-04-18
      • 2014-03-15
      • 2012-10-30
      相关资源
      最近更新 更多