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

  • Home
  • SEARCH
  • 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 6533019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:04:28+00:00 2026-05-25T10:04:28+00:00

I have models that look like this (simplified of course): class Product(models.Model): product_code =

  • 0

I have models that look like this (simplified of course):

class Product(models.Model):
    product_code = Charfield(max_length=30)

class Change(models.Model):
    product = ForeignKey(Product)

My problem is that this won’t work, because Products get deleted, but I need the reference in Change to remain (using on.delete to set it to null is not an option). So I have switched it to:

class Change(models.Model):
    product = CharField(max_length=30)

But now I have lost the benefits of a Foreign Key field when querying. When I query for a change, I cannot select_related for Products or do any other join type action without digging into extra type clauses.

So my question, is there any good way to have my cake and eat it too? To be able to join on the field easily, but not have it be an FK? Seems like there must be some way besides FK type to say that fields are related. Perhaps through a manager?

  • 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-25T10:04:28+00:00Added an answer on May 25, 2026 at 10:04 am

    I have this exact problem at hand… regrettably its priory was bumped pretty far down my list. However, I believe I have actually used a many-to-many field to retain a link in a different part of my application. Using the many-to-many field seems to address all of the issues I had come across. See my simplified models below.

    With this setup I am able to:

    • Create a Card and link a number of Card_Tests to it (note that there can be duplicates)
    • Modify or Remove any one or all of the Card_Tests without it removing the Card’s record.
    • Remove all of the Card_Tests by deleting the Card record.

    Card Model

    class Card(models.Model):
        serial_number = models.CharField(max_length=25)
        card_tests = models.ManyToManyField('Card_Test', through='Card_Test_List')
    

    Through Model

    class Card_Test_List(models.Model):
        card_id = models.ForeignKey('Card')
        card_test_id = models.ForeignKey('Card_Test')
        result       = models.TextField()
    

    Card Test Model

    class Card_Test(models.Model):
        name = models.TextField(max_length=100)
        description = models.TextField(max_length=200)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a models A and B , that are like this: class A(models.Model):
Say I have a three models that look (basically) like this: class User <
Specifically, I have a model that has a field like this pub_date = models.DateField(date
So I have a User model that :has_many other models like Documents, Videos, Posts
I have two models, Article and Post that both inherit from a base model
I have two models, Room and Image . Image is a generic model that
I have an existing app that has many, many models. I'd like to log
Generally, MVC frameeworks have a structure that looks something like: /models /views /controllers /utils
I have a few models that need to have custom find conditions placed on
Say that I have two models- Users and Accounts. Each account can have at

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.