views:

19

answers:

1

hi I want to override django.views.generic.create_update.create_object

To be more precise when a create is called on an object[here projects] via url

eg :http://localhost:8000/projects/create/

i need to check some permissions before the control gets passed to the create_object. I know decorators would help me do it .I tried using decorators but unfortunately it wasnt much successful .Can someone please tell me how exactly should i do it .It would be highly helpful Thanking in advance

A: 

What you'll need is a manager.

Details here: http://docs.djangoproject.com/en/dev/topics/db/managers/

In a nutshell: basically all models in django are governed by managers, which take care of object creation and fetching things from the database and so on. What you do is make a custom manager and assign it to the model you want to change the behaviour of.

Basically something like so:

class MyModel(models.Model):
  property = models.SomeField()

  objects = MyManager()

class MyManager(models.Manager):
      def create(self, *args, **kwargs):
          if <check permissions>:
         return super(MyManager, self).create(*args, **kwargs)
      else:
        <raise some error>
Swizec Teller