#27976: label_from_instance fails silently: form field disappears.
------------------------------------------------+------------------------
               Reporter:  Wim Feijen            |          Owner:  nobody
                   Type:  Cleanup/optimization  |         Status:  new
              Component:  Forms                 |        Version:  1.8
               Severity:  Normal                |       Keywords:
           Triage Stage:  Unreviewed            |      Has patch:  0
    Needs documentation:  0                     |    Needs tests:  0
Patch needs improvement:  0                     |  Easy pickings:  0
                  UI/UX:  0                     |
------------------------------------------------+------------------------
 Hi,

 In a forms __init__ method, I am using a foreign-key pulldown. I'd like to
 rename the labels, like this:
 {{{
        self.fields['lawyer'].label_from_instance = lambda obj:
 obj.userprofile.name
 }}}
 However, when obj has no userprofile, the code does not raise an
 Exception, as expected, but fails silently and the field disappears. I
 find this dangerous and would just expect the field to raise an Exception,
 so I can fix it, instead of causing abnormalities.

 Part of my code in more detail is here. I'm using UserProfiles because I
 find them way easier to understand than extended user models.


 {{{
 class UserProfileForm(forms.ModelForm):
     """General form to add a user. Sanne-only."""
     group =
 forms.ModelChoiceField(queryset=Group.objects.order_by('name'),
 required=True, widget=forms.RadioSelect, initial=lawyer_group)
     email = forms.EmailField(required=True, label="Email address")

     def __init__(self, *args, **kwargs):
         super(UserProfileForm, self).__init__(*args, **kwargs)

         lawyer_users = User.objects.filter(groups__name__in=['firm
 lawyer', 'client lawyer']).distinct().order_by('userprofile__name')
         self.fields['lawyer'].queryset = lawyer_users
         self.fields['lawyer'].label_from_instance = lambda obj:
 obj.userprofile.name

     class Meta:
         model = UserProfile
         fields = ['group', 'name', 'email', 'vendor', 'customer', 'city',
 'country', 'photo', 'tel', 'tel2', 'bar_city', 'bar_date_of_admission',
 'lawyer']
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27976>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.03f8cf7c823fe5021791a2628cde5740%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to