Tuesday, November 22, 2011

Re: ModelChoiceFields and verbose names

On Tuesday, 22 November 2011 21:40:29 UTC, James wrote:
Hello all:

This is my first question posted to this group! I am also a noob, so
please be gentle.

So, my issue is, I have a ModelForm, which looks like this:

class UAssumptionsForm(ModelForm):
    primary_le =
forms.ModelChoiceField(queryset=models.Mortality.objects.none())
    secondary_le =
forms.ModelChoiceField(queryset=models.Mortality.objects.none())
    def __init__(self, *args, **kwargs):
        self.insured = kwargs.pop('insured')
        super(ModelForm, self).__init__(*args, **kwargs)
        morts = self.insured.mortalities.all()
        self.fields['primary_le'].queryset = morts
        self.fields['secondary_le'].queryset = morts
    class Meta:
        model = models.UAssumptions

Now, when I render the form I get two choices for each of the fields I
overrode above. This is expected behavior.

What I want though is for each object to have a descriptive name,
rather than "Mortality object", based on the values of the object
itself, so one might have a value of "Caterpillar" and another might
have a value of "Seychelles".

I'm sure this is a common problem and I apologize in advance. I
searched, but being a noob I'm probably not using the right search
terms.

Django will take the result of the __unicode__ method on Mortality automatically.
--
DR.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/X_S7VUMBrtcJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment