Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7186115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:33:50+00:00 2026-05-28T18:33:50+00:00

I currently have a model set up like so: I have a Test class

  • 0

I currently have a model set up like so:

I have a Test class with a foreign key to a custom UserProfile class. This property is called student. So each UserProfile may have taken more than one Test. Then in turn, each UserProfile is tied to a User by the typical onetoone relation. Another level of complexity is added because I have declared two user groups tutors and students. And the UserProfile 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 list_filter = ('student',) 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 list_filter = ('student__tutors'), but I’d want this UserProfile set to be filtered down to only include those where user_groups_name=’tutors’. What is the best way to do this?

For clarity, my object model looks like this:

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')

I tried subclassing django.contrib.admin.SimpleListFilter with the following:

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

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:

http://imgur.com/lyrYk

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:33:52+00:00Added an answer on May 28, 2026 at 6:33 pm

    Interesting problem.

    I think you need to convert your lookups to a string.

    return [(str(x), y) for x, y in your_valuesqueryset]
    

    Line 98 on django.admin.filters defines the selected filter as:

    'selected': self.value() == lookup
    

    Where the lookup is populated directly from the results of the lookups method.

    The auto type coercion in your filter() call is making the filter succeed but '2' != 2

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a ModelForm set up in Django, but would like to make
I currently have a client that wants me to 'abstract' out a domain model
I currently have speakers set up both in my office and in my living
I am using asp.net mvc 2.0(default binding model) and I have this problem. I
I have an Event model with a finish_time field and a form checkbox called
I currently have a model for an assignment table in Rails 3, which looks
I have a business model with many classes in, some logical entities within this
I'm running CodeIgniter 2.0 and I have a test controller setup with this code
I currently have a project using EF 1 (.NET 3.5) and would like to
I currently have an Entity Framework model that collects data from a legacy database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.