#35542: BoundField's label and help_text and renderer should be properties not
class members
-------------------------------------+-------------------------------------
     Reporter:  Christophe Henry     |                    Owner:
         Type:                       |  Christophe Henry
  Cleanup/optimization               |                   Status:  closed
    Component:  Forms                |                  Version:  dev
     Severity:  Normal               |               Resolution:  needsinfo
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by Christophe Henry):

 Replying to [comment:1 Natalia Bidart]:

 Hello Natalia!

 Well the documentation I linked features this as the first example:

 {{{#!python
 class FooMultipleChoiceForm(forms.Form):
     foo_select = forms.ModelMultipleChoiceField(queryset=None)

     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
         self.fields["foo_select"].queryset = ...
 }}}

 The same way, you can do this, for instance:

 {{{#!python
 class FooMultipleChoiceForm(forms.Form):
     foo_select = forms.CharField()

     def __init__(self, user: User, *args, **kwargs):
         super().__init__(*args, **kwargs)
         self.fields["foo_select"].help_text = f"Indicate
 {user.first_name}'s pet name"
 }}}

 But if you do this, you won't get the result you expect when rendering
 form with `{{ form }}` since what will be rendered is
 `self["foo_select"].help_text.` (the associated `BoundField`) and not
 `self.fields["foo_select"].help_text`. In order for the help text to
 correctly be rendered, you need to write:

 {{{#!python
 class FooMultipleChoiceForm(forms.Form):
     foo_select = forms.CharField()

     def __init__(self, user: User, *args, **kwargs):
         super().__init__(*args, **kwargs)
        # Not self.field
         self["foo_select"].help_text = f"Indicate {user.first_name}'s pet
 name"
 }}}

 This is confusing and is inconsistant with the `queryset` example of the
 documentation for anyone who's not very familiar with Django's `Form` API
 and looks very much like a bug.

 My proposed solution does not introduce any breaking changes and can even
 be easily reverted to its original behavior with the patch I also proposed
 for #35542.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35542#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 django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070190595df329-c805c2a0-245f-4509-81e0-395e7102c7b9-000000%40eu-central-1.amazonses.com.

Reply via email to