为用户提供对特定对象

为用户提供对特定对象

本文介绍了Django Admin-为用户提供对特定对象/字段的访问权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为管理员进行所有者"登录.说我们有这个模型结构:

I need to make an "owners" login for the admin. Say we have this model structure:

class Product(models.Model):
    owner = models.ManyToManyField(User)
    name = models.CharField(max_length=255)
    description = models.CharField(max_length=255)
    photos = models.ManyToManyField(Photo, through='ProductPhoto')


class Photo(models.Model):
    order = models.IntegerField()
    image = models.ImageField(upload_to='photos')
    alt = models.CharField(max_length=255)


class ProductPhoto(models.Model):
    photo = models.ForeignKey(Photo)
    product = models.ForeignKey(Product)

我们有一个名为Owners的组,某些用户是其中的一部分. ProductPhotoProduct管理页面上的TabularInline.

We have a group called Owners that some users are part of. The ProductPhoto is a TabularInline on the Product admin page.

现在,所有者需要编辑权限

Now, owners need permission to edit

  1. (主要目标) 产品,其中product__in=user.products(因此,基本上只有它们拥有的产品).

  1. (primary goal) only products where product__in=user.products (so basically, only products owned by them).

(次要目标) 产品的说明照片

我将如何使用Django的管理/权限系统来做到这一点?

How would I do this with Django's admin/permission system?

推荐答案

这是行(或对象)级别的权限. Django为对象权限提供了基本支持,由您来实施代码.

This is row (or object) level permission. Django provides basic support for object permissions but it is up to you to implement the code.

幸运的是,有一些应用程序提供了嵌入式对象级权限框架. django-guardian 是我以前使用过的一个. djangopackages.com上的此页面提供了更多可以尝试的内容.

Luckily, there are a few apps that provide drop-in object-level permission framework. django-guardian is one that I have used before. This page on djangopackages.com provides some more that you can try out.

这篇关于Django Admin-为用户提供对特定对象/字段的访问权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 04:43