title: django admin list_filter foreign key subset
body: <p>I currently have a model set up like so:</p>
  
  <p>I have a <code>Test</code> class with a foreign key to a custom <code>UserProfile</code> class. This property is called <code>student</code>. So each UserProfile may have taken more than one <code>Test</code>. Then in turn, each <code>UserProfile</code> is tied to a <code>User</code> by the typical onetoone relation. Another level of complexity is added because I have declared two user groups <code>tutors</code> and <code>students</code>. And the <code>UserProfile</code> class has a ManyToMany relation on to itself labeled "tutors". The logic is that each student may have many tutors teaching him. Now what I would like to do is add filters on the right-hand side of the Test admin page that lets you filter by the student and filter by tutor as well. setting <code>list_filter = ('student',)</code> simply lists all available UserProfiles. But this would include the UserProfiles which are tied to tutors as well. Obviously I would like to filter this list down to only students, because clearly filtering by any of these tutors would result in an empty queryset. I would then like to do a similar thing with filtering by tutor, where in this case the shorthand would be <code>list_filter = ('student__tutors')</code>, but I'd want this UserProfile set to be filtered down to only include those where user_<em>groups</em>_name='tutors'. What is the best way to do this?</p>
  
  <p>For clarity, my object model looks like this:</p>
  
  <pre><code>class UserProfile(models.Model):
      user = models.OneToOneField(User, unique=True,related_name='profile')
      tutors = models.ManyToManyField("self",related_name="students")
  class Test(models.Model):
      student = models.ForeignKey(UserProfile,related_name='tests')
  </code></pre>
  
  <p>I tried subclassing <code>django.contrib.admin.SimpleListFilter</code> with the following:</p>
  
  <pre><code>class StudentListFilter(SimpleListFilter):
      title = 'student'
      parameter_name = 'student__id__exact'
      def lookups(self, request, model_admin):
          qs = model_admin.queryset(request)
          return User.objects.filter(profile__id__in=qs.values_list('student__id')).extra(select={'full_name':'first_name||" "||last_name'}).values_list('profile__id','full_name')
  
      def queryset(self, request, queryset):
          if self.value() is not None:
              return queryset.filter(student__id__exact=self.value())
          else:
              return queryset
  </code></pre>
  
  <p>Which seems to work for the first list_filter, but the trouble is for some reason there's a bug now where the selected option does not get highlighted in the custom filter. Only 'All' highlights but not the custom options. Here is an example of it that another user posted:</p>
  
  <p><a href="http://imgur.com/lyrYk" rel="nofollow">http://imgur.com/lyrYk</a></p>
  
  <p>I am currently using the development version of django 1.4, so I'm not sure if this issue is tied to that or not.</p>