jul
jul

Reputation: 37464

Permission on specific instances of a model

I've got a CustomUser model with an additional field region and other models with this same field.

How can I allow an instance of CustomUser to add/delete/view instances of the other models (give all permissions) with the same region value only?

EDIT

Using a custom Manager, as suggested by vivek soundrapandi, I can do

class ShopManager(models.Manager):
    def get_query_set(self):
        return super(ShopManager, self).get_query_set().filter(region=?)

but how can I filter the Shop instances by the region of the currently connected user?

And this still allow the CustomUser to modify Shop instances from other region using the default Manager, right?

There is no way to do it using Django permission system?

Thanks

Upvotes: 0

Views: 255

Answers (2)

Vivek S
Vivek S

Reputation: 5540

Try using your own functions using model managers. click here for docs explanation

OK,

class Blog(models.Model):
    name = models.CharField(max_length=100)
    tagline = models.TextField()

class Entry(models.Model):
    blog = models.ForeignKey(Blog)
    name = models.CharField(max_length=100)
Entry.objects.filter(blog__name__exact='Beatles Blog')

This example retrieves all Entry objects with a Blog whose name is 'Beatles Blog'. So, your model manager should just perform the same for all the other classes that you want to refer to and combine them to produce a single result set.If i have understood your problem correct,this should be the solution.

Upvotes: 0

DrTyrsa
DrTyrsa

Reputation: 31951

Use django-guardian (actually that's it, but SO wants more characters)

Upvotes: 2

Related Questions