[Django] #27976: label_from_instance fails silently: form field disappears.

26 views
Skip to first unread message

Django

unread,
Mar 22, 2017, 2:48:29 AM3/22/17
to django-...@googlegroups.com
#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.

Django

unread,
Mar 22, 2017, 2:49:55 AM3/22/17
to django-...@googlegroups.com
#27976: label_from_instance fails silently: form field disappears.
-------------------------------------+-------------------------------------

Reporter: Wim Feijen | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Forms | Version: 1.8
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
-------------------------------------+-------------------------------------
Description changed by Wim Feijen:

Old description:

New description:

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 from
the form output. 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']
}}}

The form is outputted as divs using crispy_forms

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27976#comment:1>

Django

unread,
Mar 22, 2017, 7:52:56 AM3/22/17
to django-...@googlegroups.com
#27976: label_from_instance fails silently: form field disappears.
-------------------------------------+-------------------------------------

Reporter: Wim Feijen | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Forms | Version: 1.8
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 Tim Graham):

Can you reproduce without crisy_forms and tell us where the exception
catching happens?

--
Ticket URL: <https://code.djangoproject.com/ticket/27976#comment:2>

Django

unread,
Mar 22, 2017, 5:53:03 PM3/22/17
to django-...@googlegroups.com
#27976: label_from_instance fails silently: form field disappears.
-------------------------------------+-------------------------------------

Reporter: Wim Feijen | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Forms | Version: 1.8
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 kapil garg):

I tested it without crispy_form on dev version and it does throw an
Exception. Here are the code samples i used


{{{
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question_text


class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text

class TestForm(ModelForm):
def __init__(self, *args, **kwargs):
super(TestForm, self).__init__(*args, **kwargs)
self.fields['question'].label_from_instance = lambda obj: obj.name
class Meta:
model = Choice
fields = ['question', 'choice_text']
}}}

So i assume, crispy_forms has to do something with that behaviour.

--
Ticket URL: <https://code.djangoproject.com/ticket/27976#comment:3>

Django

unread,
Mar 22, 2017, 6:26:37 PM3/22/17
to django-...@googlegroups.com
#27976: label_from_instance fails silently: form field disappears.
-------------------------------------+-------------------------------------

Reporter: Wim Feijen | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Forms | Version: 1.8
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 Wim Feijen):

Thanks for looking into this! Then I'll post this at crispy_forms and I
will close this ticket.

--
Ticket URL: <https://code.djangoproject.com/ticket/27976#comment:4>

Django

unread,
Mar 22, 2017, 6:26:51 PM3/22/17
to django-...@googlegroups.com
#27976: label_from_instance fails silently: form field disappears.
-------------------------------------+-------------------------------------

Reporter: Wim Feijen | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Forms | Version: 1.8
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Wim Feijen):

* status: new => closed
* resolution: => invalid


--
Ticket URL: <https://code.djangoproject.com/ticket/27976#comment:5>

Reply all
Reply to author
Forward
0 new messages