Hi!

I have an application where I need a field for selecting between some
predefined options that are dynamic up to the point where the form is
instantiated. So I've used a TypedChoiceField and overwritten __init__
on the form (not the field) to set the choices based on a database
lookup and some massaging.

This works well, but now I'd like to use the same field on another
form.

I thought I could solve this by defining a custom field, but the
problem is that on Field.__init__, it's far to early to capture the
choices. And the next call, clean(), is after the fact. Ideally I'd be
able to get called when the form is constructed.

What do you think about adding an empty form_constructed() to Field to
be called by BaseForm.__init__() after self.fields has been set?


Then a custom field could look like this

class MyField(forms.ChoiceField):
    def __init__(self, foo, **kwargs):
        self.foo = foo
        super(self.__class__, self).__init__(**kwargs)

    def form_constructed(self, form):
        if self.foo:
            self.choices = ...

I had a look at ModelChoiceField, and it seems like a lot of non-
intuitive code could be deleted from it with a hook like this?


Ole

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

Reply via email to