Tuesday, August 30, 2011

Check related object attribute in a view

This is probably very simple, but I've been hitting walls ever since I
started...

I have a model A that contains a link to another model B. B has an
"owner" attribute. This owner attribute is translated as a per object
permission using guardian.

Models:

class Service (models.Model):
name = models.CharField(max_length=128, unique=True)
owner = models.ForeignKey(User)

class Deliverable (models.Model):
project = models.ForeignKey(Project)
service = models.ForeignKey(Service)

I would like to allow editing Deliverable only if the current user is
allowed to change the corresponding Service. To that end, I tried to
subclass UpdateView:

class UpdateDeliverableView(UpdateView):

def dispatch(self, *args, **kwargs):
# Check guardian permissions here
return super(ValidateServiceView, self).dispatch(*args,
**kwargs)

in order to check guardian permissions, I need to access the Service
from this view. But I can't seem to find how to access the
Deliverable: self.get_object() does not work (complains that
UpdateDeliverableView has no 'kwargs') How am I supposed to do it ?

I also need the current user but self.request.user complains that self
has no 'request'

Does anyone have an hint as to how I could do that ?

Thanks in advance :)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment