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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:06:37+00:00 2026-05-22T01:06:37+00:00

So I have a model, Comment. In it, it must keep a reference to

  • 0

So I have a model, Comment. In it, it must keep a reference to whatever it’s commenting to.
It can be a response to a blog post, or it can be a response to another comment, etc.

So how do I go about storing that relationship? Typically, I would just store the information with a ForeignKey. But a ForeignKey requires that it know the type of model it’s referencing.

Is there something built into Django like a ForeignKey that can reference any type of model? If not, what are the best ways of implementing such a relationship?

Here’s what I’ve thought of:

I could use an integer to store the id of the object that it is responding to, and then a CharField to store the type and then I would obtain the object by doing something like globals()[type_name].objects.get(id=id) but I think I would have some problems down the road if I ever needed to do anything complex like searching if I used that method.

Alternatively, I could create a different Comment class for each object that it could be responding to (automatically, of course). But again, that causes limitations. I could no longer easily do things like Comment.objects.get(id=5)

Or I could have my comment class have a ForeignKey for each possible thing it could be responding to, leaving all but 1 null for each comment. Still seems like a sub-par solution.

Suggestions?

  • 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-22T01:06:37+00:00Added an answer on May 22, 2026 at 1:06 am

    Check out GenericForeignKey in the built-in contenttypes framework.

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

Sidebar

Related Questions

Suppose I have an Comment model that belongs_to a Post model. I want to
I have a comment model with string property like this: [Column] public string Text
I have just upgraded to IE9 RC (which i must add isnt too bad
we have model helper (used by several different models) called set_guids that sets self.theguid
Domain Driven Design can be really confusing at times and since I am rather
Before starting, I do have a very particular question and if you want to
Here is my situation: User A must monitor the results of an sql query
I am running Ruby on Rails 3 and I have an application that makes
I want to use CKEditor for a comment field in my ASP.NET MVC 2
Consider this simplified view of some code with which I'm working: @Stateless(...) @Remote(...) @TransactionAttribute(TransactionAttributeType.MANDATORY)

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.