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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:07:20+00:00 2026-05-30T18:07:20+00:00

I need to access to ForeignKey ‘s model of a given model. Here’s the

  • 0

I need to access to ForeignKey‘s model of a given model.

Here’s the sample code:

class modelA(models.db):
    field1 = models.ForeignKey('modelB')
class modelB(models.db):
    pass
## below is the pseudo code
modelA.get_model_of_fk('field1').objects.all() # fetch all objects of modelB

Of course it will be so easy if there’s an instance of modelA involved. But I just can’t find a way to do it without creating an instance of modelA.

My current solution:

instance_of_a = modelA.objects.all()[0]
model_b = instance_of_a.field1.__class__
model_b.objects.all() # fetch all objects of modelB
  • 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-30T18:07:22+00:00Added an answer on May 30, 2026 at 6:07 pm

    Django’s ContentType framework is built for this. You can refer the docs here.

    In your case,

    from django.contrib.contenttypes.models import ContentType
    model_type = ContentType.objects.get_for_model(modelA.field1)
    model_type.model_class().objects.all() # this will get you all objects of the foreign key model
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need access to the uint64_t typedef from stdint.h in some wrapper code that
Consider the Models class definition: In models.py, class Classroom(models.Model): subject1 = models.CharField(maX_length=80) subject2 =
Let's say I have a set of Django Models: class Article(models.Model): title = models.CharField(max_length=100,
I have these models: class App(models.Model): name = models.CharField(max_length=100) class ProjectA(models.Model): name = models.CharField(max_length=100)
I have the following models: class UserProfile(models.Model): user = models.OneToOneField(User) score = models.PositiveIntegerField() class
If you have some models: class Teacher(models.Model): name = models.CharField(max_length=50) class Student(models.Model): age =
I have three models linked by foreign keys: class One(models.Model): ... class Two(models.Model): one
I have 2 Django models : class A(models.Model): uniq_name = models.CharField(max_length=30,primary_key=True) info1 = models.CharField(max_length=30)
I have the following model in Django: from django.db import models class User(models.Model): pen_name
I need access to the ApplicationID property of the parent class in order to

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.