Hello,

I'd like to propose a new feature for forms.

Currently if you need a form field with some extra features you can just
subclass from django.forms.Field and add all the features you need.

However as soon as you're in a template and want to access the field you
will get a BoundField instead of your custom field. The need to
differentiate BoundField and Field is clear but it would be nice if it
was possible to also customize BoundField.

For this reason I propose adding a new method "bind_to_form()" to
django.forms.Field. It takes a form and a name and returns a BoundField
specific to the Field instance. The default implementation just returns
an instance of the existing BoundField but now it is possible to
customize it.

To make it easier, BoundField should become public API. That means it
should be able to import it with "from django.forms import BoundField"
in order to subclass it.

In [1] you can see an implementation of bind_to_form().
I also wrote BoundChoiceField as an example that allows better access to
the choices. In [2] you can see how this could be used.

Considering BoundField wasn't really public before, this change
shouldn't bring any backwards incompatibilities since the default
behaviour stays the same. In my test branch all tests pass.

I'd really like to hear your feedback on this.
If this sounds reasonable I will start writing documentation and
possibly drafting a DEP if that's needed.

Moritz

[1]: https://github.com/MoritzS/django/tree/bound-fields
[2]: https://gist.github.com/MoritzS/7bd792f2eaf37da28dfb

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/55C5E947.2010809%40googlemail.com.
For more options, visit https://groups.google.com/d/optout.

Attachment: signature.asc
Description: OpenPGP digital signature

  • A... 'Moritz Sichert' via Django developers (Contributions to Django itself)
    • ... Tim Graham
      • ... 'Moritz Sichert' via Django developers (Contributions to Django itself)
        • ... Tim Graham
          • ... Preston Timmons
            • ... 'Moritz Sichert' via Django developers (Contributions to Django itself)
              • ... Tim Graham
                • ... 'Moritz Sichert' via Django developers (Contributions to Django itself)
                • ... Preston Timmons
                • ... Tim Graham
                • ... 'Moritz Sichert' via Django developers (Contributions to Django itself)

Reply via email to