views:

157

answers:

1

My question is very similar to this one: http://stackoverflow.com/questions/1057252/django-how-do-i-access-the-request-object-or-any-other-variable-in-a-forms-clea

Except, I have the same problem with admin form. So I can't see a way to init the form myself, therefore - to pass a request to it.

Thanks beforehand.

+1  A: 

There are a number of hooks in the ModelAdmin class to allow you to do things this - look at the code in django.contrib.admin.options.

Two methods that might help you are ModelAdmin.save_form and ModelAdmin.save_model, both of which are passed the request object. So you can override these methods in your Admin subclass and do any extra processing you need.

Edited after comment

You're quite right that this won't let you validate the form dependent on the user's privileges. Unfortunately the form instantiation is buried deep within the add_view and change_view methods of ModelAdmin.

There aren't many possibilities without duplicating a lot of existing code. You could override the *_view methods; or you could try and override the modelform_factory function to return a new class with the request object baked in already; or you could try fiddling with the form class __new__ method to do the same thing, but that's tricky because of the form metaclass.

Daniel Roseman
Daniel, thanks for your answer! Actually, I've tried overriding these methods, berfore implementing forms... But - us I understood (from one of your answers, if I'm not mistaking) there is no way to perform validation from these methods. By "validation", I mean ability not to save some content if I don't like something and at the same time ability to give some valuable feedback. My task is - validation of admin input depending on user rights. May be I'm digging in a wrong place?
Wiseman
See my extended answer above.
Daniel Roseman
Many thanks again, for a good bunch of fishing rods :).
Wiseman