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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:13:57+00:00 2026-05-15T06:13:57+00:00

My models are set up as follows (this is an example and not my

  • 0

My models are set up as follows (this is an example and not my actual models)

class modelA(Model):
   field1 = CharField(max_length=50)

class modelB(modelA):
   field2 = CharField(max_length=50)

class anotherModel(Model):
   connection = models.ForeignKey(modelA)
   title = CharField(max_length=50)

Would I be able to have an connection to modelB stored in anotherModel since modelB inherits from model A.

mod_b = modelB()
conn_b =  anotherModel()
conn_b.connection = mod_b

If not how would I handle this?

Thanks

  • 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-15T06:13:58+00:00Added an answer on May 15, 2026 at 6:13 am

    The Generic Relations feature from Django’s built-in ContentTypes module is the most supported way to handle polymorphic foreign keys.

    You will need to add some supporting fields to your model so that the framework can figure out which particular class a foreign key represents, but other than that it will handle loading the correct type fairly transparently.

    In your case, it would be something like:

    from django.contrib.contenttypes.models import ContentType
    from django.contrib.contenttypes import generic
    
    # modelA and modelB as before
    
    class anotherModel(Model):
        connection_content_type = models.ForeignKey(ContentType)
        connection_object_id = models.PositiveIntegerField()
        connection = generic.GenericForeignKey('connection_content_type',
            'connection_object_id')
    

    Note that you don’t need to set/read the connection_content_type or connection_object_id fields yourself… the generics framework will handle that for you, they just need to be there for the generics to work.

    mod_a = modelA()
    mod_b = modelB()
    
    conn =  anotherModel()
    conn.connection = mod_b
    conn.save()
    conn.connection = mod_a # change your mind
    conn.save()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have my models set up already. class books(models.Model): title = models.CharField... ISBN
I've got a set of models that look like this: class Page(models.Model): title =
I have a two models set up like this: class User < ActiveRecord::Base #
Lets say I have some view models set up as follows: public class User
I have the following models set up: class Contact < ActiveRecord::Base belongs_to :band belongs_to
Currently my application caches models in memcache like this: memcache.set(somekey, aModel) But Nicks' post
JSTL variable values are not shown in EL. For example this code: <%@ taglib
I'm trying to create an HR that follows the below model: -----<title>------------------------------------------------- This is
I'm working on a blog application in Django. Naturally, I have models set up
I have set up a few models in a simple django project. When I

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.