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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:38:50+00:00 2026-05-27T10:38:50+00:00

I have two models with a foreign key relationship between them. In the admin,

  • 0

I have two models with a foreign key relationship between them. In the admin, the edit page for the model with the foreign key relationship described (Model No. 1) displays a ModelChoiceField. The page for the other side of the relationship (Model No. 2) displayed nothing, until I added the first model to the ModelAdmin as an inline. The inline gives me the option of creating a new object from Model No. 1.

I want to add a ModelChoiceField to the inline on Model No. 2 so that users can choose between creating a new object or selecting from a list of pre-existing ones.

Ideally, I would also be able to use a filter to populate the new ModelChoiceField for Model No. 1 objects.

  • 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-27T10:38:51+00:00Added an answer on May 27, 2026 at 10:38 am

    Okay, asking this question got me nothing but the cool tumbleweed badge for my profile. I eventually discovered the following solution. It’s simpler than I expected but it left me asking another question here because, once implemented, selecting from the ModelChoiceField on the admin page and saving does not create the foreign key relationship as expected.

    Anyway, on the the solution:

    My Art model contains the boolean field “has_storypak” to indicate whether it has a relationship to and instance of the Storypak model. Since I expected Art instances to only related to one Storypak while Storypaks could have many associated artworks, I wrote the following custom field to only contain instances for which the value for “has_storypak” was False.

    class RuntimeArtSelectForm(forms.ModelForm):
        storypak_orphan = forms.ModelChoiceField(label="Art",
                queryset=Art.objects.filter(has_storypak=False))
        class Meta:
            model = Art
            fields = ('storypak_orphan',)
    

    Next I added this form to an inline form for the Art model…

    class ArtInline(admin.TabularInline):
        model = Art
        form = RuntimeArtSelectForm
    

    … and included the ArtInline in the ModelAdmin for Storypak. This gave me the drop-down containing the filtered list of model objects I was looking for. However I still have the problem mentioned above and this open question looking for a solution.

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

Sidebar

Related Questions

I have a simple one-to-many (models.ForeignKey) relationship between two of my model classes: class
I have to map two simple table with a foreign key relationship. One of
I have the following two models: School and User, and a HABTM relationship between
I have two classes, Portfolio, and PortfolioImage. class PortfolioImage(models.Model): portfolio = models.ForeignKey('Portfolio', related_name='images') ...
I have two models, Article and Post that both inherit from a base model
I have two models. We'll call them object A and object B. Their design
I have two models, Room and Image . Image is a generic model that
I have two tables that model an inheritance relationship. This is expressed by a
So I recently created an HABTM relationship between two models (project & user). Before,
If I have two objects that have a many-to-many relationship, I would typically model

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.