﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
20202	BaseModelForm uses model_to_dict to provide initial, causes problems for ModelChoiceField with non-pk to_field_name	valtron2000@…	nobody	"{{{
class M(models.Model):
  slug = models.CharField(max_length = 5, unique = True)
  other = models.ForeignKey('M', blank = True, null = True)

class F(forms.ModelForm):
  class Meta:
    model = M
    fields = ('other',)
  
  other = forms.ModelChoiceField(to_field_name = 'slug')

def example():
  m1 = M.objects.create(slug = 'xyzzy')
  m2 = M.objects.create(slug = 'abcde', other = m1)
  form = F(instance = m2)
  
  # Prints m1.pk; should contain the model instance, instead,
  # so that the ModelChoiceField can actually get the to_field_name attribute
  print form.initial['other']
  
  # Prints this: (note that xyzzy isn't selected=""selected"")
  # <tr><th><label for=""id_other"">Other:</label></th><td><select id=""id_other"" name=""other"">
  # <option value="""">---------</option>
  # <option value=""xyzzy"">M object</option>
  # <option value=""abcde"">M object</option>
  # </select></td></tr>
  print form
}}}

There is a workaround: manually provide the correct field in the initial."	New feature	closed	Forms	1.5	Normal	duplicate		bmispelon@…	Accepted	0	0	0	0	0	0
