'A formset over a ForeignKey with a to_field can be saved. Regression for #10243'

def test_formset_over_to_field(self):

    Form = modelform_factory(User, fields=u'__all__')
    FormSet = inlineformset_factory(User, UserSite, fields=u'__all__')
    form = Form()
    form_set = FormSet(instance=User())
    data = {u'serial': u'1', u'username': u'apollo13', u'usersite_set-TOTAL_FORMS': u'1', u'usersite_set-INITIAL_FORMS': u'0', u'usersite_set-MAX_NUM_FORMS': u'0', u'usersite_set-0-data': u'10', u'usersite_set-0-user': u'apollo13'}
    user = User()
    form = Form(data)
    if form.is_valid():
        user = form.save()
    else:
        self.fail((u'Errors    found    on    form:%s' % form_set))
    form_set = FormSet(data, instance=user)
    if form_set.is_valid():
        form_set.save()
        usersite = UserSite.objects.all().values()
        self.assertEqual(usersite[0][u'data'], 10)
        self.assertEqual(usersite[0][u'user_id'], u'apollo13')
    else:
        self.fail((u'Errors    found    on    formset:%s' % form_set.errors))
    data = {u'usersite_set-TOTAL_FORMS': u'1', u'usersite_set-INITIAL_FORMS': u'1', u'usersite_set-MAX_NUM_FORMS': u'0', u'usersite_set-0-id': six.text_type(usersite[0][u'id']), u'usersite_set-0-data': u'11', u'usersite_set-0-user': u'apollo13'}
    form_set = FormSet(data, instance=user)
    if form_set.is_valid():
        form_set.save()
        usersite = UserSite.objects.all().values()
        self.assertEqual(usersite[0][u'data'], 11)
        self.assertEqual(usersite[0][u'user_id'], u'apollo13')
    else:
        self.fail((u'Errors    found    on    formset:%s' % form_set.errors))
    data = {u'usersite_set-TOTAL_FORMS': u'2', u'usersite_set-INITIAL_FORMS': u'1', u'usersite_set-MAX_NUM_FORMS': u'0', u'usersite_set-0-id': six.text_type(usersite[0][u'id']), u'usersite_set-0-data': u'11', u'usersite_set-0-user': u'apollo13', u'usersite_set-1-data': u'42', u'usersite_set-1-user': u'apollo13'}
    form_set = FormSet(data, instance=user)
    if form_set.is_valid():
        form_set.save()
        usersite = UserSite.objects.all().values().order_by(u'data')
        self.assertEqual(usersite[0][u'data'], 11)
        self.assertEqual(usersite[0][u'user_id'], u'apollo13')
        self.assertEqual(usersite[1][u'data'], 42)
        self.assertEqual(usersite[1][u'user_id'], u'apollo13')
    else:
        self.fail((u'Errors    found    on    formset:%s' % form_set.errors))
