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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:35:13+00:00 2026-05-27T08:35:13+00:00

I have two tables in my database (django: models in my app) as follows:

  • 0

I have two tables in my database (django: models in my app) as follows:

class Model1(models.Model):
    name = models.CharField()
    #etc....

class Model2(models.Model):
    link = models.ForeignKey(Model1)

Each Model1 can have many instances of Model2 linked to it, but Model2 can only be linked to one Model1 – a basic one-to-many relationship.

My problem is this: I need each Model1 to have a primary Model2 – that is, one of it’s related Model2s needs to somehow be marked as “primary”.

These are my thoughts so far: should I do this with

  1. A Boolean field on Model2 that says whether it’s primary, and then app-level validation that says only one Model2 per Model1 can have that field set True, or
  2. A ForeignKey field on Model1 called “primary_model2” that links to a Model2 instance, with app-level validation that says only a Model2 that is linked to Model1 can be it’s primary

Any ideas?? I’m kind of stumped. Thanks in advance!

  • 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-27T08:35:14+00:00Added an answer on May 27, 2026 at 8:35 am

    I would go with a boolean on Model2. It’s very easy to do a save override that would set any other instances of Model2 that are related to Model1 as false.

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

Sidebar

Related Questions

I have the following model in django: class Node(models.Model): name = models.CharField(max_length=255) And this
I have the following two models: class Position(models.Model): position = models.CharField(max_length=100) class UserProfile(models.Model): user
I have two database tables, Team ( ID, NAME, CITY, BOSS, TOTALPLAYER ) and
I have two tables in mysql database groups id|name _______ 1 |red 2 |blue
I have two tables in my database: Products id (int, primary key) name (varchar)
I have two tables in the database Team:Id(int PK), Name(nvarchar) and Player:Id(int PK), Name(nvarchar),
I have two tables in my database as following: Employee Table: Username, Name, Job,
I have two tables in database: A: [ ID, name, SECRET_KEY] examplary rows: ID1,
I have two models 'Company' and 'Vehicle' And two tables in database named same
I have two database tables with the following structure: actions: action_id int(11) primary key

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.