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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:28:56+00:00 2026-05-31T07:28:56+00:00

I have Chart and Module models (see code below). Each chart belongs to a

  • 0

I have Chart and Module models (see code below). Each chart belongs to a module (via a ForeignKey). A chart may have another chart as its parent (another ForeignKey). What I would like in the admin is that the dropdown for parent on a particular chart only includes charts in the same module.

I’m looking for a Python solution, not AJAX. That means that on creating a new chart the dropdown will have to be empty (without a model instance there’s no module selected) and that changing the module in the admin won’t update the parent options to match until the model is saved. I’m ok with that.

There are plenty of similar-sounding questions (and answers) that turn out to filter the options according to the user; ModelAdmin.formfield_for_foreignkey gets the request as an argument so you can use request.user, but it doesn’t get given a model instance to play with. (I’m using Django 1.3.)

My models (highly simplified of course):

class Module(models.Model):
    pass

class Chart(models.Model):
    module = models.ForeignKey(Module)
    parent = models.ForeignKey(Chart, blank=True, null=True)

class ChartAdmin(admin.ModelAdmin):
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        # I would like to do this, but have no "instance":
        kwargs['queryset'] = Chart.objects.filter(module=instance.module)
        return super(ChartAdmin, self).formfield_for_foreignkey(self, db_field, request, **kwargs)

admin.site.register(Chart, ChartAdmin)
  • 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-31T07:28:57+00:00Added an answer on May 31, 2026 at 7:28 am

    Just override the ModelForm being used:

    class ChartAdminForm(forms.ModelForm):
        def __init__(self, *args, **kwargs):
            super(ChartAdminForm, self).__init__(*args, **kwargs)
            if self.instance.module_id:
                self.fields['parent'].queryset = self.fields['parent'].queryset.filter(module=self.instance.module)
    
    class ChartAdmin(admin.ModelAdmin):
        form = ChartAdminForm
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Drupal module creating a page via hook_menu(). I am trying to
I have a chart in a Worksheet in Excel and I have a macro
I have a chart with a series that denotes a large set (1000's) of
I have a chart (in bitmap format) that I'm trying to render to a
In my MVC project I have a chart being generated on my view as
I have a line chart that is updated every so and so seconds, similar
I have a pie chart in BIRT and about 80 data points. I'm looking
RS2008 - pie chart I have 'outside' labels with lines pointing to the segment
I am playing with matplotlib - I have a bar chart, and I want
I have to display a column chart in a user form in VBA. Exporting

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.