#34034: Adding a class on ChoiceWidget subwidgets is excessingly difficult
------------------------------+--------------------------------------
     Reporter:  Claude Paroz  |                    Owner:  nobody
         Type:  New feature   |                   Status:  new
    Component:  Forms         |                  Version:  dev
     Severity:  Normal        |               Resolution:
     Keywords:                |             Triage Stage:  Unreviewed
    Has patch:  0             |      Needs documentation:  0
  Needs tests:  0             |  Patch needs improvement:  0
Easy pickings:  0             |                    UI/UX:  0
------------------------------+--------------------------------------

Comment (by Claude Paroz):

 That means that you have to subclass the Django widget. Then for
 subwidgets, you have to override `create_option`, not `get_context`. Then
 you have to take care not overwriting any existing class content. So in
 some code of mine, I had to write:
 {{{
     def create_option(self, *args, attrs=None, **kwargs):
         attrs = attrs.copy() if attrs else {}
         if 'class' in attrs:
             attrs['class'] += ' form-check-input'
         else:
             attrs.update({'class': 'form-check-input'})
         return super().create_option(*args, attrs=attrs, **kwargs)
 }}}
 and note that the class name is hardcoded, so if you need another class
 for another part of a form, you have to set the class name as a subclass
 attribute, and create other widget subclasses. And as that can touch
 `RadioSelect`, `CheckboxSelectMultiple`, etc., you'll have to create
 subclasses for all of them (if used, of course).

 That's what I mean by "excessingly difficult". But it's doable, of course.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34034#comment:2>
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070183700d7c63-a40c39db-6383-4cb7-a50b-acb385526e0d-000000%40eu-central-1.amazonses.com.

Reply via email to