Re: Choices as a callable.. but no parameters?

2015-04-20 Thread Stephen J. Butler
On Mon, Apr 20, 2015 at 9:45 PM, Vijay Khemlani  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

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAD4ANxWy%2Bs351iJcZHxqJgDQScMHa%3DNvbc5uqUqCTXmz6QEawA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Choices as a callable.. but no parameters?

2015-04-20 Thread Vijay Khemlani
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.

On Mon, Apr 20, 2015 at 10:56 PM, Steve Hiemstra 
wrote:

> I am trying to build a form with dynamic choices. e.g., the choices should
> depend on the user.
>
> https://docs.djangoproject.com/en/1.8/ref/forms/fields/#choicefield
>
> Says choices may now be a callable, and sure enough it is called in `
> django/forms/fields.py(818)` – but without any parameters.
>
> How can I get access to the current user, view, or any bit of context.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/4a74be7f-e3cc-4e72-9a15-6a1ca43c9677%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CALn3ei1O%3DXvKT7G%3DkwW4TjZvLpT5iw%2BmdBkwux%2B5bGZkidxfiQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Choices as a callable.. but no parameters?

2015-04-20 Thread Steve Hiemstra
I am trying to build a form with dynamic choices. e.g., the choices should 
depend on the user.

https://docs.djangoproject.com/en/1.8/ref/forms/fields/#choicefield

Says choices may now be a callable, and sure enough it is called in `
django/forms/fields.py(818)` – but without any parameters.

How can I get access to the current user, view, or any bit of context.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/4a74be7f-e3cc-4e72-9a15-6a1ca43c9677%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.