#24788: Allow Forms to set a default prefix attribute
-----------------------------+--------------------
     Reporter:  kezabelle    |      Owner:  nobody
         Type:  New feature  |     Status:  new
    Component:  Forms        |    Version:  master
     Severity:  Normal       |   Keywords:
 Triage Stage:  Unreviewed   |  Has patch:  0
Easy pickings:  0            |      UI/UX:  0
-----------------------------+--------------------
 Example of what I'd like to be able to do:
 {{{
 from django import forms
 class MyForm(forms.Form):
     field = ChoiceField(...)
     prefix = 'myform-is-awesome'

 >>> form_itself = MyForm(data=request.GET, files=None)
 >>> form_itself.prefix
 'myform-is-awesome'
 }}}
 However this isn't currently workable, because
 [https://github.com/django/django/blob/master/django/forms/forms.py#L86
 self.prefix] is always bound based on the argument given in the arguments,
 the default of which is `None`

 By hoisting the prefix to be a class attribute, and then doing
 {{{
 if prefix is not None:
     self.prefix = prefix
 }}}
 it seems like forms which ship with apps could effectively namespace
 themselves such that N overlapping form fields could be POSTed at once and
 resolved to the correct form.

 However, that's only a suggested use-case that hints at why it might be
 useful to have; the reason I specifically needed this was for validating
 whether a GET request was likely my filtering form, '''before'''
 instantiating and validating the data:
 {{{
 filtered_form = any(x.startswith(MyForm.prefix) for x in
 request.GET.keys())
 }}}
 so that I can selectively reset session-saved filters only when a new
 filterset is done (before then saving validated submitted data back into
 the session).

 My workaround ended up looking like:
 {{{
 class MyForm(forms.Form):
     prefix = 'woo'
     def __init__(self, *a, *kw):
         super(MyForm, self).__init__(*a, **kw)
         self.prefix = self.__class__.prefix
 }}}
 which seems a bit ... rubbish.

--
Ticket URL: <https://code.djangoproject.com/ticket/24788>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.5963c5dd986ee767123cbff246d4e5ef%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to