# Using a specific Model Field for Choices

**URL:** <https://forum.djangoproject.com/t/using-a-specific-model-field-for-choices/23762>\
**Category:** Forms & APIs\
**Created:** [September 10, 2023, 4:04am UTC](https://forum.djangoproject.com/t/using-a-specific-model-field-for-choices/23762 "2023-09-10T04:04:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![onyeibo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/onyeibo/32/11494_2.png) [@onyeibo](https://forum.djangoproject.com/u/onyeibo)\
**Post date:** [September 10, 2023, 4:04am UTC](https://forum.djangoproject.com/t/using-a-specific-model-field-for-choices/23762/1 "2023-09-10T04:04:04Z")

</div>

I have a CreateView that renders the following form:

```auto
class PackageForm(forms.ModelForm):
    class Meta:
        """Meta Class for Form"""
    
        model = Package
        fields = " __all__"
        labels = {"servicefk": "Service"}

```

The form creates a model instance (Package). The **Package** model connects to **Service** model (see below) viia a foreign key (servicefk):

```auto
class Service(models.Model):
    name = models.TextField()
    abbreviation = models.CharField(max_length=255)
    
    def __str__ (self):
        """A textual name for terminal outputs"""
        
        return self.abbreviation

```

The view renders fine except for one thing: It renders the abbreviation field as choices for the Select input element. I want the full “name” field instead. Apparently, django is using the output of the ` __str__ ()` to construct the choices. What is the quickest way to list the name field instead … without altering ` __str__ ()`? Please don’t send me to the docs … I am experiencing a mental block

---

<div class="post-metadata">

**Author:** ![antoinehumbert](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/antoinehumbert/32/14851_2.png) [@antoinehumbert](https://forum.djangoproject.com/u/antoinehumbert)\
**Post date:** [September 10, 2023, 8:27am UTC](https://forum.djangoproject.com/t/using-a-specific-model-field-for-choices/23762/2 "2023-09-10T08:27:58Z")

</div>

You can define a custom ModelField for the `servicefk` field. E.g.:

```auto
from django.forms import ModelChoiceField

class ServiceModelChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.name

```

Then in your ModelForm, define:

```auto
class PackageForm(forms.ModelForm):
    class Meta:
        """Meta Class for Form"""
    
        model = Package
        fields = " __all__"
        field_classes = {"servicefk": ServiceModelChoiceField}
        labels = {"servicefk": "Service"}

```

---

<div class="post-metadata">

**Author:** ![onyeibo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/onyeibo/32/11494_2.png) [@onyeibo](https://forum.djangoproject.com/u/onyeibo)\
**Post date:** [September 17, 2023, 6:53am UTC](https://forum.djangoproject.com/t/using-a-specific-model-field-for-choices/23762/3 "2023-09-17T06:53:40Z")

</div>

That was amazing! Where can I find documentation on ModelChoiceFields and field\_classes? I would like to know more about those.

> [@antoinehumbert](#):
>
> `filed_classes = {"servicefk": ServiceModelChoiceField}`

I realized you meant "field\_classes’ when your sample didn’t work. It worked perfectly after I adapted it.

---

<div class="post-metadata">

**Author:** ![antoinehumbert](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/antoinehumbert/32/14851_2.png) [@antoinehumbert](https://forum.djangoproject.com/u/antoinehumbert)\
**Post date:** [September 17, 2023, 7:12am UTC](https://forum.djangoproject.com/t/using-a-specific-model-field-for-choices/23762/4 "2023-09-17T07:12:45Z")

</div>

Sorry for the typo error. I corrected my answer.

You can find information about ModelForm’s fields overrides here ([Creating forms from models | Django documentation | Django](https://docs.djangoproject.com/en/4.2/topics/forms/modelforms/#overriding-the-default-fields)) and ModelChoiceField here ([Form fields | Django documentation | Django](https://docs.djangoproject.com/en/4.2/ref/forms/fields/#modelchoicefield)) where it is explained that

The **str** () method of the model will be called to generate string representations of the objects for use in the field’s choices. To provide customized representations, subclass ModelChoiceField and override label\_from\_instance.
