Alex Rivera | Logout

Object Ownership in Django

Asked 2009-06-16T00:09:49.477
12

I'm wondering how I might accomplish a simple 'object ownership' system with django models, such that, by default, only the owner of an object may edit it.

I am attempting to allow a 'Management' group to edit all objects on behalf of the object owners, and have at this point added a custom permission:

class Meta:
    permissions     = (
        ("manage_object", "Can manage objects"),
    )

To establish 'ownership' I've toyed with the idea of adding a def to the model:

def owner(self):
    return self.user

But then, how might I go further? I could implement the permissions in a view and display relevant UI with a template, i.e.:

if request.user is object.owner:
    # ... do stuff
elseif request.user.has_perm.can_manage:  # this line is probably not right
    # ... do something else

... and then present different UI elements on a template level.

So, the question is:

  • what faults/benefits are there with this approach?
  • are there recommendations?
  • or, any other previously implement methods?

Best thanks!

Edit
Report

1 Answer

2

A while back I wrote up the usual technique for doing this in the admin. You may want to read through that to see how the implementation works.

answered 2009-06-19T20:13:31.327

Your Answer