与Django中的其他用户共享对象
我正在Django为一个相当复杂的系统建模。我将只在这里发布它的相关部分,我将展示简化的用例图,以更好地表达我的想法 我基本上有两种类型的用户:卖家和客户与Django中的其他用户共享对象,django,django-models,django-users,django-permissions,django-guardian,Django,Django Models,Django Users,Django Permissions,Django Guardian,我正在Django为一个相当复杂的系统建模。我将只在这里发布它的相关部分,我将展示简化的用例图,以更好地表达我的想法 我基本上有两种类型的用户:卖家和客户 卖方“获得了客户,这意味着卖方现在拥有 客户的个人信息,并可以与他/她互动。 卖家无法与他没有获得的客户进行互动。 卖家创建相关对象的模型层次结构(子级别中的每个模型都通过外键连接到其父级) 卖家与一些客户共享创建的框对象及其所有相关对象 授权客户可以: R-读取框的一些属性 R-ead和U-更新项目模型的一些属性 无法访问项的某些属
卖家无法与他没有获得的客户进行互动。
- R-读取框的一些属性
- R-ead和U-更新项目模型的一些属性
- 无法访问项的某些属性模型
- A:在用户之间建立关系的最佳方法是什么?
我发现这可能有用 - B:在Django中共享模型实例的最佳方法是什么
与其他用户一起使用?
在我的情况下,默认情况下,如果没有明确共享,客户无法访问卖方创建的任何模型李>- C:如果我只共享框模型,这是否意味着它的所有相关模型(框模型外键)也将共享
- D:控制字段级别权限的最佳方法是什么 每个用户?
在这里可以用吗
Seller->Customer
# This example assumes that both customers and sellers have user table entries.
from django.contrib.auth.models import User
class Customer(User): pass
class Seller(User):
acquired_customers = ManyToManyField(Customer,
related_name="acquired_sellers")
def acquire(customer):
" A convenience function to acquire customers "
self.acquired_customers.add(customer.id)
B:在Django中与其他用户共享模型实例的最佳方法是什么?
您可以使用ManyToManyField
的自定义“直通”模型来添加要跟踪的额外信息。在本例中,我们添加了卖方,并在共享时自动添加时间戳。这可以让你做一些事情,比如显示与你共享的产品,按它们共享的时间排序,以及发送给你的卖家的名称
# Install mptt for heirararchical data.
from mptt.models import MPTTModel
class Box(MPTTModel):
" Nestable boxen for your Items "
owner = ForeignKey(Seller)
title = CharField(max_length=255)
shared_with = ManyToManyField(Customer,
related_name='boxes_sharedwithme', through=SharedBox)
class Item(Model):
" A shareable Item "
box = ForeignKey(Box)
title = CharField(max_length=255)
class SharedBox(Model):
" Keeps track of who shares what to whom, and when "
when = DateTimeField(auto_now_add=True)
box = ForeignKey(Box)
seller = ForeignKey(Seller)
customer = ForeignKey(Customer)
#----------------------------
# share an Item with a Customer
def share_item(request, box_id, customer_id, **kwargs):
# This will fail if the user is not a seller
seller = request.user.seller
# This will fail if the seller is not the owner of the item's box
box = Box.objects.get(
id=box_id, owner=seller)
# This will fail if the seller has not acquired the customer
customer = Customer.objects.get(
id=customer_id, acquired_sellers=seller)
# This will share the item if it has not already been shared.
SharedBox.objects.create_or_update(
box=box, seller=seller, customer=customer)
return HttpResponse("OK")
C:如果我只共享盒子模型,这是否意味着它的所有相关模型(盒子模型的外键)也将被共享?
隐式权限是“业务逻辑”,这意味着您可能需要自己实现它。幸运的是,Django的权限系统是可插入的,因此您可以添加自己的规则,在层次结构中递归检查权限。或者,您可以创建自定义管理器,在使用的任何位置向查询添加适当的规则
from django.db.models import Manager
from django.db.models.query import EmptyQuerySet
class ItemManager(Manager):
def visible(user):
iqs = self.get_query_set()
oqs = EmptyQuerySet()
# add all the items a user can see as a seller
try: oqs |= iqs.filter(box__owner=user.seller)
except Seller.DoesNotExist: pass
# add all the items a user can see as a customer
try: oqs |= iqs.filter(box__shared_with=user.customer)
except Customer.DoesNotExist: pass
# return the complete list of items.
return oqs
class Item(Model): objects = ItemManager()
class ItemListView(ListView):
model = Item
def get_queryset(request):
return self.model.objects.visible(request.user)
D:控制每个用户的字段级权限的最佳方法是什么?
如果这需要超级粒度或每个用户,那么django guardian就是一个不错的选择。如果权限是基于规则的,那么最好使用一个简单的字段,以降低数据库查询的复杂性
class Property(Model):
title = CharField(max_length=255)
units = CharField(max_length=10,
choices=UNIT_TYPES, null=True, blank=True)
# -- A simple field that toggles properties for all users
class ItemProperty(Model):
item = ForeignKey(Item)
property = ForeignKey(Property)
value = CharField(max_length=100)
customer_viewable = BooleanField(default=False)
customer_editable = BooleanField(default=False)
# -- A simple field that defines user classes who can view/edit
from django.contrib.auth.models import Group
class ItemProperty(Model):
item = ForeignKey(Item)
property = ForeignKey(Property)
value = CharField(max_length=100)
viewable_by = ForeignKey(Group)
editable_by = ForeignKey(Group)
免责声明:这是我对这个问题的看法。Django社区内部对授权的看法严重分歧。永远不会有一个“最好”的方式来做任何事情,所以仔细考虑你是否需要一些真正的通用性,并能负担得起数据库点击,或者你是否需要快速和特定业务,并能承受失去灵活性。谢谢,非常深刻的答案!我想知道,关于问题D:如果我不在模板中公开对象(比如一个项目)的一些属性,如果我不在客户可以访问的任何形式中包含这些属性,这基本上意味着客户将永远无法访问这些属性,对吗?所以这是我不应该关心的事情。我猜。只要这些属性没有对应的可猜测url,并且该视图没有检查权限,那么不,您就不必担心它。