title: Form validation: "That choice is not one of the available choices."
body: <p>One of my models has a foreign key field. This field is displayed, as expected, as a drop-down (Select widget). The POSTed value for that field is a string, although the related model's ID has a default type primary key (integer):</p>
  
  <p><a href="https://i.stack.imgur.com/IjRkQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IjRkQ.png" alt="enter image description here"></a></p>
  
  <p>Therefore, form validation fails while cleaning the data with:</p>
  
  <blockquote>
    <p>Select a valid choice. That choice is not one of the available choices.</p>
  </blockquote>
  
  <p>I have no custom form definition or validation at all. Why is the foreign key provided via select widget in form not converted to the right type?</p>
  
  <h2>EDIT</h2>
  
  <p>The Model:</p>
  
  <pre><code>class Expansion(models.Model):
  
      created = models.DateTimeField(auto_now_add=True, verbose_name=_("Created"), help_text='created on')
  
      modified = models.DateTimeField(auto_now=True, verbose_name=_("Modified"), help_text='Last modified')
  
      size = models.PositiveIntegerField(verbose_name=_("Size"),
                                         help_text=_('Specify the size in GB to increase in multiple of 50'),
                                         validators=[validate_expansion_size])
  
      lun = models.ForeignKey(LUN, verbose_name=_('LUN'), help_text=_('Which LUN to expand'))
  
      user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('User'), help_text=_('Associate User'),
                               db_column='user_id')
  
      output = models.TextField(verbose_name=_("Output"), help_text=_('The Result from the expansion'))
  
      def __unicode__(self):
          return self.lun.vdisk_uuid
  
      class Meta:
          ordering = ('-created',)
          verbose_name = _('LUN Expansion')
          verbose_name_plural = _('LUN Expansions')
  
      # This model is backed by a REST endpoint, using django-rest-models
      class APIMeta:
          db_name = 'svc'
          resource_path = 'expansions/'
          resource_name = 'expansion'
          resource_name_plural = 'expansions'
  </code></pre>
  
  <p>The View:</p>
  
  <pre><code>class ExpansionCreateView(SuccessMessageMixin, CreateView):
      model = Expansion
      template_name = 'expansion/create.html'
      form_class = ExpansionCreateForm
      success_url = reverse_lazy('expansion:index')
      success_message = _("LUN successfully expanded")
  
      def get(self, request, *args, **kwargs):
          if not PSC.objects.filter(user=self.request.user).exists():
              messages.warning(self.request, _("You have not set a payment method yet. Please do this first."))
              return HttpResponseRedirect("{0:s}?next={1:s}".format(reverse('payment:create'), reverse('expansion:create')))
  
          if not valid_email(self.request.user.email):
              messages.warning(self.request, _("You have not set a valid email address yet. Please do this first."))
              return HttpResponseRedirect("{0:s}?next={1:s}".format(reverse('account_email'), reverse('expansion:create')))
  
          return super(ExpansionCreateView, self).get(request, *args, **kwargs)
  
      def form_valid(self, form):
          form.instance.user = self.request.user
          # print(form, file=sys.stderr)
          return super(ExpansionCreateView, self).form_valid(form)
  </code></pre>
  
  <p>The Form:</p>
  
  <pre><code>class ExpansionCreateForm(forms.ModelForm):
  
      submit_text = _('Expand LUN')
  
      def __init__(self, *args, **kwargs):
          super(ExpansionCreateForm, self).__init__(*args, **kwargs)
  
          self.helper = FormHelper(self)
  
          self.helper.form_id = 'id-a2goform'
          self.helper.form_class = 'form-horizontal form-label-left'
          self.helper.label_class = 'col-lg-2'
          self.helper.field_class = 'col-lg-8'
          self.helper.form_method = 'post'
          self.helper.help_text_inline = True
          self.helper.filter_by_widget(forms.Select).wrap(Field, css_class="select2_single ")
          self.helper.filter_by_widget(forms.CheckboxInput).wrap(Field, css_class="js-switch")
          self.helper.add_input(Submit('submit', self.submit_text))
  
      class Meta:
          model = Expansion
          fields = ['lun', 'size']
  </code></pre>
  
  <p>The template:</p>
  
  <pre><code>{% extends 'content.html' %}
  
  {% load staticfiles %}
  {% load crispy_forms_tags %}
  {% load i18n %}
  
  {% block title %}{% trans "Expansion" %}{% endblock %}
  
  {% block headline %}{% trans "Expand LUN" %}{% endblock %}
  {% block subheadline %}{% trans "Expand" %}{% endblock %}
  
  {% block subcontent %}
      {% crispy form %}
  {% endblock %}
  
  {% block page_inline_js %}
      &lt;script&gt;
          $(document).ready(function () {
              $(".select2_single").select2();
          });
      &lt;/script&gt;
  {% endblock %}
  </code></pre>