'Ensure that a filter on a FK respects the FK\'s to_field attribute.
Refs #17972.'

def test_fk_with_to_field(self):

    modeladmin = EmployeeAdmin(Employee, site)
    request = self.request_factory.get(u'/', {})
    changelist = self.get_changelist(request, Employee, modeladmin)
    queryset = changelist.get_queryset(request)
    self.assertEqual(list(queryset), [self.jack, self.john])
    filterspec = changelist.get_filters(request)[0][(-1)]
    self.assertEqual(force_text(filterspec.title), u'department')
    choices = list(filterspec.choices(changelist))
    self.assertEqual(choices[0][u'display'], u'All')
    self.assertEqual(choices[0][u'selected'], True)
    self.assertEqual(choices[0][u'query_string'], u'?')
    self.assertEqual(choices[1][u'display'], u'Development')
    self.assertEqual(choices[1][u'selected'], False)
    self.assertEqual(choices[1][u'query_string'], u'?department__code__exact=DEV')
    self.assertEqual(choices[2][u'display'], u'Design')
    self.assertEqual(choices[2][u'selected'], False)
    self.assertEqual(choices[2][u'query_string'], u'?department__code__exact=DSN')
    request = self.request_factory.get(u'/', {u'department__code__exact': u'DEV'})
    changelist = self.get_changelist(request, Employee, modeladmin)
    queryset = changelist.get_queryset(request)
    self.assertEqual(list(queryset), [self.john])
    filterspec = changelist.get_filters(request)[0][(-1)]
    self.assertEqual(force_text(filterspec.title), u'department')
    choices = list(filterspec.choices(changelist))
    self.assertEqual(choices[0][u'display'], u'All')
    self.assertEqual(choices[0][u'selected'], False)
    self.assertEqual(choices[0][u'query_string'], u'?')
    self.assertEqual(choices[1][u'display'], u'Development')
    self.assertEqual(choices[1][u'selected'], True)
    self.assertEqual(choices[1][u'query_string'], u'?department__code__exact=DEV')
    self.assertEqual(choices[2][u'display'], u'Design')
    self.assertEqual(choices[2][u'selected'], False)
    self.assertEqual(choices[2][u'query_string'], u'?department__code__exact=DSN')
