A: What is the best approach for creating user relationships?
All users are not created equal. django-relationship creates arbitrary relationships between arbitrary users, which is most likely not what you want. Are you sure you want to limit this relationship strictly 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)
Q: What is the best approach for sharing model instances in Django with other users?
You can use the ManyToManyField custom “ ManyToManyField -through” model to add additional information that you want to track. In this case, we add a seller and an automatic timestamp when it was provided. This allows you to do things such as displaying products that you have shared, sorted by their sharing along with the name of the seller who sent it to you.
# 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)
C: If I only share the Box model, does it implicitly mean that all related models (#Foreign keys to Box model) will also be shared?
Implicit permissions are “business logic,” which means you will most likely have to implement it yourself. Fortunately, Django's permission system is plugged in, so you can add your own rules that return a hierarchy to check permissions. Or you can create custom managers who add the appropriate rules to the request wherever they are used.
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()
D: What is the best approach for managing field level resolution for each user?
If this should be super-granular or for every user, then django-guardian is the way to go. If permissions are rule-based, then you might be better off with a simple field to reduce the complexity of database queries.
class Property(Model): title = CharField(max_length=255) units = CharField(max_length=10, choices=UNIT_TYPES, null=True, blank=True)
Disclaimer: This is my opinion on this. The views on authorization in the Django community are highly fragmented. There will never be a single “better” way to do something, so think carefully whether you need something really common and can afford to get into the database, or you need something fast and business-specific and can afford loss of flexibility.