# Set Initial value to a ModelChoiceField

**URL:** <https://forum.djangoproject.com/t/set-initial-value-to-a-modelchoicefield/28624>\
**Category:** Forms & APIs\
**Created:** [March 2, 2024, 12:23pm UTC](https://forum.djangoproject.com/t/set-initial-value-to-a-modelchoicefield/28624 "2024-03-02T12:23:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![minuxdev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/minuxdev/32/18524_2.png) [@minuxdev](https://forum.djangoproject.com/u/minuxdev)\
**Post date:** [March 2, 2024, 12:23pm UTC](https://forum.djangoproject.com/t/set-initial-value-to-a-modelchoicefield/28624/1 "2024-03-02T12:23:52Z")

</div>

How do I set `inital` value to `field1` in order to remove those dashes that are selected by default when rendering this form?

```auto
class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = ("field1", ...)
        widgets = {
            "field1": forms.RadioSelect,
        }

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 2, 2024, 12:32pm UTC](https://forum.djangoproject.com/t/set-initial-value-to-a-modelchoicefield/28624/2 "2024-03-02T12:32:20Z")

</div>

See the last bullet point **above** the examples at [A full example](https://docs.djangoproject.com/en/5.0/topics/forms/modelforms/#a-full-example)

Also see the last paragraph before the section for “Enumeration types” at [Field.choices](https://docs.djangoproject.com/en/5.0/ref/models/fields/#django.db.models.Field.choices)

---

<div class="post-metadata">

**Author:** ![minuxdev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/minuxdev/32/18524_2.png) [@minuxdev](https://forum.djangoproject.com/u/minuxdev)\
**Post date:** [March 2, 2024, 12:59pm UTC](https://forum.djangoproject.com/t/set-initial-value-to-a-modelchoicefield/28624/3 "2024-03-02T12:59:29Z")

</div>

Thank you for pointing it out. What I forgot to mention is that `field1` is a `ForeignKey` to other model which means is type of `ModelChoiceField`, Is it possible to still explicitly set default value to this field in `MyModel` as mentioned in the bullet point you pointed [here](https://docs.djangoproject.com/en/5.0/topics/forms/modelforms/#a-full-example)?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 2, 2024, 1:05pm UTC](https://forum.djangoproject.com/t/set-initial-value-to-a-modelchoicefield/28624/4 "2024-03-02T13:05:30Z")

</div>

Yes. How you do it depends upon whether that default value is a known constant.  
If it is, you can specify it as the default value in the form field definition. If it’s not, then you can initialize the value for that form field in the form’s ` __init__ ` method.

---

<div class="post-metadata">

**Author:** ![minuxdev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/minuxdev/32/18524_2.png) [@minuxdev](https://forum.djangoproject.com/u/minuxdev)\
**Post date:** [March 2, 2024, 1:32pm UTC](https://forum.djangoproject.com/t/set-initial-value-to-a-modelchoicefield/28624/5 "2024-03-02T13:32:26Z")

</div>

`If it’s not, then you can initialize the value for that form field in the form’s __init__ method`  
Due the fact that value is not a known constant I set initial value to the field which works, however, I don’t know how to get rid of these `-----`.  
Here is what I did

in `forms.py`

```auto
    def __init__ (self, condition=None, *args, **kwargs):
        super(). __init__ (*args, **kwargs)
        if condition:
            queryset = MyModel.objects.filter(field1=condition)
            self.fields["field1"].queryset = queryset
            self.fields["field1"].initial = queryset[0]

```

in `views.py` I passed the `condition` as follow

```auto
    form = MyForm(condition=some_condition)

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 2, 2024, 3:02pm UTC](https://forum.djangoproject.com/t/set-initial-value-to-a-modelchoicefield/28624/6 "2024-03-02T15:02:57Z")

</div>

The “-----” are explained in the docs I referenced above for `Field.choices`.

---

<div class="post-metadata">

**Author:** ![Dasch](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dasch/32/22159_2.png) [@Dasch](https://forum.djangoproject.com/u/Dasch)\
**Post date:** [August 26, 2024, 10:29am UTC](https://forum.djangoproject.com/t/set-initial-value-to-a-modelchoicefield/28624/7 "2024-08-26T10:29:16Z")

</div>

I don’t know if this is the best solution. For anyone with the same problem:

`models.py`:

```auto
class MyModel(models.Model):
    field1 = models.ForeignKey(OtherModel, on_delete=models.CASCADE, blank=False, default="")
    # default is not used, but needs to be set as KenWhitesell mentioned

```

`forms.py`:

```auto
class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = ["field1"]
    def __init__ (self, condition=None, *args, **kwargs):
        super(). __init__ (*args, **kwargs)
        if condition:
            queryset = MyModel.objects.filter(field1=condition)
            self.fields["field1"].queryset = queryset
            if queryset:
                self.fields["field1"].initial = queryset[0]

```
