Honnisha
Honnisha

Reputation: 53

I need display ModelChoiceField like CharField

I have some form modelformset_factory with model contains ForeignKey, but I need display this ForeignKey(ModelChoiceField) like CharField.

I use like that:

class SingleNeedFormWithDescription(ModelForm):
    helper = StandardFormHelper()
    helper.template = 'template.html'
    need = CharField()

    class Meta:
        model = NeedMembershipOrganization
        fields = ['need', 'description']

I have id of need in my template, but I need need.title or need.__str__().

My model:

class NeedMembershipOrganization(Model):
    need = ForeignKey('needs.Need')
    organization = ForeignKey(Organization)
    description = TextField(blank=True, null=True, verbose_name='description')

Thanks!

Upvotes: 0

Views: 606

Answers (1)

Håken Lid
Håken Lid

Reputation: 23064

You can change the ModelChoiceField widget to a TextInput, but you might have to figure out some way to validate and parse the input.

class SingleNeedFormWithDescription(ModelForm):

    need = forms.ModelChoiceField(
        queryset=Need.objects.all(), 
        widget=forms.TextInput)

Upvotes: 3

Related Questions