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 842291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:56:10+00:00 2026-05-15T05:56:10+00:00

There’s photologue application, simple photo gallery for django, implementing Photo and Gallery objects. Gallery

  • 0

There’s photologue application, simple photo gallery for django, implementing Photo and Gallery objects.
Gallery object has ManyToMany field, which references Photo objects.

I need to be able to get list of all Photos for a given Gallery. Is it possible to add Gallery filter to Photo’s admin page?
If it’s possible, how to do it best?

  • 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-15T05:56:11+00:00Added an answer on May 15, 2026 at 5:56 am

    You need to write a custom FilterSpec! Custom Filter in Django Admin on Django 1.3 or below
    It’ll look like this:

    from django.contrib.admin.filterspecs import RelatedFilterSpec, FilterSpec
    from models import Gallery
    
    class GalleryFilterSpec(RelatedFilterSpec):
        def __init__(self, f, request, params, model, model_admin):
            self.lookup_kwarg = f.name
            self._lookup_model = f.rel.to
            self.lookup_val = request.GET.get(self.lookup_kwarg, None)
            self.user = request.user
            self.lookup_choices = [(g.pk, g.name) for g in Gallery.objects.all()]
    
        def has_output(self):
            return len(self.lookup_choices) > 1
    
        def title(self):
            return self._lookup_model._meta.verbose_name
    
    FilterSpec.filter_specs.insert(0, 
            (lambda f: f.rel.to == Gallery, GalleryFilterSpec))
    

    Put it in a module filters.py in your app package and import it in you admin.py (it’s important to import it, so that the filter becomes registered on the admin site!)

    EDIT: “f” is the field instance, in this case models.ManyToManyField The last line registers the FilterSpec for all fields that have a relation to the Gallery model. This will not work as you mentioned if the field is defined on the Gallery model, since django.contrib.admin.views.main.ChangeList.get_filters checks if the field you define in the list really exist on the model (doesnt work for related_name either). I think the easiest way around is that you could make a custom template for that changelist and hardcode your filter in there, the FilterSpec itself isn’t need for the filtering itself, django uses just the url get parameters for that!

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

Sidebar

Related Questions

There is an application written in PHP which I am converting to Ruby. When
There are few apps like Strava which records users movements using GPS. It also
There are several shading languages available today like GLSL, HLSL, CG, which one to
There will be 500+ threads concurrently uploading an unique object to a bucket all
There's a bug in FireBug: I accidentally clicked where the line numbers are, which
There is a website called Gild.com that has different coding puzzles/challenges for users to
There is a div (form) which opens on the click of a button. There
There are two field in my form. 1) Html select field item ( From
There must be a simple solution to this, but after 4 hours of browsing
There are numerous Agile software development methods. Which ones have you used in practice

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.