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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:40:11+00:00 2026-06-04T04:40:11+00:00

I am trying to filter a m-2-m based on some 3 way relational logic.

  • 0

I am trying to filter a m-2-m based on some 3 way relational logic. I have the following models (example only… but the more I write it, it looks like a game I’d like to play…) Forgive my over use of the spam and eggs metavariables…

Models:

class SuperProfile(models.Model):
    user = models.ForgeignKey('User')
    eggs_unlocked = models.ManyToMany('Egg')
    my_kitchen = models.ForeignKey('SuperKitchen')

class SuperKitchen(models.Model):
    name = models.CharField(max_length=20)

class Egg(models.Model):
    eggyness = models.PostiveIntegerField(help_text=_("how Eggy it is. eg." 
                                                      "Spam'n'Eggs is over 9000")
    kitchens = models.ForeignKey(help_text=_('What kitchen carries this')

Question: As an administrator, in someone else’s SuperProfileAdmin:

  • I want to list what eggs they can use based on what kitchen they are part of
  • A person’s Kitchen doesn’t change, ever.
  • Eggs only appear in one kitchen.
  • How do you get an instance of the SuperProfile or SuperKitchen to filter the Egg list for said other user

I’m not sure if this is clear, please comment on what needs clarification.

  • 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-06-04T04:40:12+00:00Added an answer on June 4, 2026 at 4:40 am

    In your EggAdmin, you must override queryset method

    class EggAdmin(admin.ModelAdmin):
        ...
        def queryset(self, request):
            kitchen = request.user.superprofile_set.get().my_kitchen #get related users kitchen
            qs = super(EggAdmin, self).queryset(request) #call original queryset method that you are overriding
            return qs.filter(kitchens=kitchen) #apply your filter
    

    UPDATE: Ok, that change everything… On SuperPrifile admin, when you open a SuperProfile record, you wish eggs_unlocked to be filtered according to that user… So:

    import re
    # grab the superprofile id from the url
    sup_pro_rgx=re.compile(r'(\d+)')
    sup_pro = sup_pro_rgx.findall(request.META['REQUEST_URI'])[0]
    # I know this is really the ugliest way to do this, but there is no other way (at least as far as i know) to do this
    
    
    class SuperProfileAdmin(admin.ModelAdmin):
    ...
    def formfield_for_manytomany(self, db_field, request, **kwargs):
        if db_field.name == "eggs_unlocked":
            my_kitchen = self.get_object(request, object_id=sup_pro).my_kitchen
            kwargs["queryset"] = Egg.objects.filter(kitchen=my_kitchen)
        return super(SuperProfileAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)
    

    I know, using regex to grab the object id is a really bad practise, but as i mention, that is the only way to do this as i know.

    And here is the doc for formfield_for_manytomany

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

Sidebar

Related Questions

I have been trying to filter tcpdump output based on packets lengths. But I
I have a selection of elements that I'm trying to filter down based on
I'm trying to filter some XML in JavaScript using E4X and have some specific
I have very long select query which i need to filter based on some
I'm trying to filter button events based on whether they have a CSS class
Looking for an example where I can filter my collection based on some filtering
So I have been using in some macros coding to filter rows based on
I am trying to filter out some text based on regex like phone* means
Im trying to filter rows based on a column say c1 that contains boolean
My site has a bunch of widgets and i'm trying to filter them based

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.