# Overriding model's choices in form \_\_init\_\_

**URL:** <https://forum.djangoproject.com/t/overriding-models-choices-in-form-init/15048>\
**Category:** Forms & APIs\
**Created:** [July 26, 2022, 5:01pm UTC](https://forum.djangoproject.com/t/overriding-models-choices-in-form-init/15048 "2022-07-26T17:01:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![andyewen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/andyewen/32/7864_2.png) [@andyewen](https://forum.djangoproject.com/u/andyewen)\
**Post date:** [July 26, 2022, 5:01pm UTC](https://forum.djangoproject.com/t/overriding-models-choices-in-form-init/15048/1 "2022-07-26T17:01:54Z")

</div>

I’m having an issue where I want to override the choices on a CharField dynamically request by request. I’m setting `.choices` on the field in the form’s ` __init__ ` method. This updates the choices that are rendered into the form but when the form is used to validate `request.POST`, the value seems to be getting validated against the original list of choices from the model instead of the override choices.

Here are some example files that demonstrate the issue:

**models.py**

```auto
from django.db import models

class Widget(models.Model):
    name = models.CharField(max_length=50)
    country_code = models.CharField(max_length=2, choices=[
        ('GB', 'United Kingdom'), ('US', 'United States'), ('CA', 'Canada'), ('AU', 'Australia'),
    ])

```

**forms.py**

```auto
from django import forms

from .models import Widget

class WidgetForm(forms.ModelForm):
    class Meta:
        model = Widget
        fields = [
            'name',
            'country_code',
        ]

    def __init__ (self, *args, **kwargs):
        super(). __init__ (*args, **kwargs)

        self.fields['country_code'].choices = [
            ('DE', 'Germany'),
            ('FR', 'France'),
            ('ES', 'Spain'),
            ('IT', 'Italy'),
        ]

```

Am I going about this wrong? Any help would be much appreciated! 🙂

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [July 26, 2022, 7:07pm UTC](https://forum.djangoproject.com/t/overriding-models-choices-in-form-init/15048/2 "2022-07-26T19:07:38Z")

</div>

hey there!  
Are you getting any errors? The code is failing when you try to save, or when `is_valid` is returning False?

---

<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:** [July 27, 2022, 2:09am UTC](https://forum.djangoproject.com/t/overriding-models-choices-in-form-init/15048/3 "2022-07-27T02:09:56Z")

</div>

It would help if you also provided the view here.

---

<div class="post-metadata">

**Author:** ![andyewen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/andyewen/32/7864_2.png) [@andyewen](https://forum.djangoproject.com/u/andyewen)\
**Post date:** [August 2, 2022, 1:52pm UTC](https://forum.djangoproject.com/t/overriding-models-choices-in-form-init/15048/4 "2022-08-02T13:52:59Z")

</div>

`is_valid` is returning `False` when I set country code to one of the ones specified in the form’s ` __init__ ` method.

---

<div class="post-metadata">

**Author:** ![andyewen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/andyewen/32/7864_2.png) [@andyewen](https://forum.djangoproject.com/u/andyewen)\
**Post date:** [August 2, 2022, 1:55pm UTC](https://forum.djangoproject.com/t/overriding-models-choices-in-form-init/15048/5 "2022-08-02T13:55:34Z")

</div>

Here’s the view I’m using. `is_valid` returns `False` when I set `country_code` to one of the ones specified in the form’s ` __init__ ` method in `request.POST`.

**views.py**

```auto
from django.shortcuts import redirect, render

from .forms import WidgetForm
from .models import Widget

def index(request):
    form = WidgetForm()
    if request.method == 'POST':
        form = WidgetForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect(request.path_info)

    widgets = Widget.objects.all()
    return render(request, 'app/index.html', {'form': form, 'widgets': widgets})

```

---

<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:** [August 2, 2022, 2:13pm UTC](https://forum.djangoproject.com/t/overriding-models-choices-in-form-init/15048/6 "2022-08-02T14:13:03Z")

</div>

The issue here appears to be that while the _form_ validation would pass, the _model_ validation fails. The submitted data must pass both validators to be considered valid.

See [Model field reference | Django documentation | Django](https://docs.djangoproject.com/en/4.0/ref/models/fields/#django.db.models.Field.choices) and [Model instance reference | Django documentation | Django](https://docs.djangoproject.com/en/4.0/ref/models/instances/#validating-objects) for more details.

As a result, about the most you can do at the form level would be to restrict the list of available choices, not replace them.

---

<div class="post-metadata">

**Author:** ![andyewen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/andyewen/32/7864_2.png) [@andyewen](https://forum.djangoproject.com/u/andyewen)\
**Post date:** [August 4, 2022, 4:42pm UTC](https://forum.djangoproject.com/t/overriding-models-choices-in-form-init/15048/7 "2022-08-04T16:42:50Z")

</div>

I didn’t think the choices on the model field did any validation by itself. I don’t get any sort of error if I manually set `widget.country_code` to something that’s not in the list of choices defined on the model.

Does the form do two rounds of validation? One round against the choices defined on the form field itself and another round against the choices defined on the form field’s associated model field’s choices?

---

<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:** [August 4, 2022, 4:44pm UTC](https://forum.djangoproject.com/t/overriding-models-choices-in-form-init/15048/8 "2022-08-04T16:44:45Z")

</div>

Your questions are all addressed in the two docs referenced in my previous response.

---

<div class="post-metadata">

**Author:** ![andyewen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/andyewen/32/7864_2.png) [@andyewen](https://forum.djangoproject.com/u/andyewen)\
**Post date:** [August 4, 2022, 5:05pm UTC](https://forum.djangoproject.com/t/overriding-models-choices-in-form-init/15048/9 "2022-08-04T17:05:30Z")

</div>

I apologise for not fully reading the links. I had no idea that models could have any sort of validation built in. I also see that, unless used manually, the model’s clean methods are only ever used by `ModelForm`.

I guess I can get round this either by defining all the possible choices at model level and only reducing them down in the form or by only defining the choices at the form level and not giving the model’s CharField choices.

Thanks for your help!
