--
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 django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
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/d936a1d5-3f6e-4190-83c3-5cc31c8fbb4e%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 developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
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/56eb4ce1-b0d5-443c-bd6a-c230fc41c6b5%40googlegroups.com.
--
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 django-developers+unsubscribe@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
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/CAC6Op18ifztiCqCm1Vr39%2BNGJJpCnu8a3gmYZ8sBMXyGtFZwSg%40mail.gmail.com.
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
Apologies for the formatting.
https://gist.github.com/melvyn-sopacua/dbf3c8f71023d6fc261131cb0a851f58
--
Melvyn Sopacua
What are the next steps? Shall I create a pull request, or does this need more consensus first?