如何为Django管理站点创建自定义权限

发布于 2025-02-11 00:29:44 字数 404 浏览 2 评论 0 原文

默认情况下,当我创建模型Django时,就会给出权限,例如

add/change/delete/view 

我将上述任何权限授予特定用户,则该用户可以从该模型中查看所有对象。但是我想向他们展示与外国钥匙相关的对象。

假设

我有一个书籍模型

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey(
        User, on_delete=models.CASCADE
    )

,如果我想授予作者许可,他们只能从Django管理员中查看他们的链接书籍。

我该怎么做?

By default when I create a model Django gives permissions like

add/change/delete/view 

If I give any of the above permission to a specific user then that user can view all the objects from that model. But I want to show them only objects linked with foreign keys.

Let's Say,

I have a Book model

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey(
        User, on_delete=models.CASCADE
    )

If I want to give authors permission, they can only view their linked books from the Django admin.

How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故乡的云 2025-02-18 00:29:45

一切都发生在Django Meta类中。只需按照 django documantation> django documantation

class Meta:
        permissions = [
            ("change_task_status", "Can change the status of tasks"),
            ("close_task", "Can remove a task by setting its status as closed"),
        ]

您要操纵默认许可,请按照这些将此添加到您的模型中。

Everything happens in the Django Meta class. Just follow what the Django Documantation says:

class Meta:
        permissions = [
            ("change_task_status", "Can change the status of tasks"),
            ("close_task", "Can remove a task by setting its status as closed"),
        ]

If you want to manipulate the default permission, you follow these documentation explanation an add this to you model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文