On Monday 06 March 2017 10:10:41 David Seddon wrote:
> Hi all,
> 
> One thing I've always found challenging with Django is to adjust the
> functionality of forms from other apps.  An example might be to add an
> extra field to a login form provided by a third party module.
> 
> What I end up doing is often overriding the urlconf, view and form
> class. Or, worse, monkey patching the form class.
> 
> A much nicer way to do this would be to add a few well chosen signals
> to forms.
> 
> Potential ones could be:
> 
> - post_init

Putting in a +1 and use case for pre_init: Inject dynamically generated 
form.prefix.

Right now I have to inject PrefixedFormView in every view using it and 
carefully watch 
my mro of already complex views.

Use case is having several modals containing forms on a single page ("Edit 
this", "Add 
related", "New this"). They need to be prefixed to deal with naming conflicts 
since 
id_{fieldname} will not be page unique anymore.

With the signal, I can do away with the view mixin and the WrappedForm is 
solely 
responsible for it.
I have no preference for the technology used (signal, hook, injection, new 
buzzword), 
as long as the net result is that the object creating the prefix can handle the 
prefix for 
all views.

Is there a ticket yet?

Use case code:

*class PrefixedFormView(*generic.FormView*):    def post(*self, request, 
***args, 
****kwargs*):        *self.prefix *= *request.GET.get*('prefix'*, *None)        
return 
/super/()*.post*(*request, ***args, ****kwargs*)*

*class WrappedForm(/object/):    def __init__(*self, form_class*: *BaseForm, 
action_url*: *str,                 is_multipart*: *bool *= False*, 
model_instance*: *Model 
*= None*,                 initial*: *dict *= None*, prefix*: *str *= 'auto'*, 
****kwargs*):*
*<snipped for brevity>

*        *if *prefix *== 'auto':            *self.prefix *= *uuid4*()*.hex      
  *else:            
*self.prefix *= *prefix        self.form *= None        
*self._set_query_prefix*()

    def _set_query_prefix(*self*):        *final_url *= 
*urlparse*(*self.action_url*)        
*query *= *QueryDict*(*query_string*=*final_url.query, mutable*=True)        
*query[*'prefix'*] *= *self.prefix        parts *= (*final_url.scheme, 
final_url.netloc, 
final_url.path,                 final_url.params, query.urlencode*()*, 
final_url.fragment*)        
*self.action_url *= *urlunparse*(*parts*)


*

-- 
Melvyn Sopacua

-- 
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 https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/1561133.ak0JmRi9Co%40devstation.
For more options, visit https://groups.google.com/d/optout.

Reply via email to