On Mon, Apr 20, 2015 at 9:45 PM, Vijay Khemlani <
vkhe...@gmail.com> wrote:
> Overwrite the constructor of the form (__init__), pass the user or whatever
> parameters you may need and overwrite the choices of the field in the body
> of the method.
This is the same strategy I've used to filter ForeignKey fields on the
current user. If you're using class based views you should override
get_form_kwargs() to add the user parameter to the form's __init__().
It's a little trickier if you want to make this work in a ModelAdmin
subclass though. I don't see a similar method to override. What I've
done is to override ModelAdmin.get_form() like this:
def get_form(self, request, obj=None, **kwargs):
form_class = super(MyModelAdmin, self).get_form(request, obj, **kwargs)
class _Form(form_class):
def __init__(form_self, *args, **kwargs):
super(_Form, form_self).__init__(user=request.user, *args, **kwargs)
return _Form