Opened 8 years ago

Closed 8 years ago

#27976 closed Cleanup/optimization (invalid)

label_from_instance fails silently: form field disappears.

Reported by: Wim Feijen Owned by: nobody
Component: Forms Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Wim Feijen)

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

Change History (5)

comment:1 by Wim Feijen, 8 years ago

Description: modified (diff)

comment:2 by Tim Graham, 8 years ago

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

comment:3 by kapil garg, 8 years ago

I tested it without crispy_form on dev version and also 1.8 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.

Last edited 8 years ago by kapil garg (previous) (diff)

comment:4 by Wim Feijen, 8 years ago

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

comment:5 by Wim Feijen, 8 years ago

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top