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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:56:56+00:00 2026-06-04T06:56:56+00:00

I would like to reflect on a model and list all its backward generic

  • 0

I would like to reflect on a model and list all its backward generic relations.

My model looks like this:

class Service(models.Model):
    host = models.ForeignKey(Host)

    statuses = generic.GenericRelation(Status)

The Status object looks like this:

class Status(TrackedModel):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey()

    class Meta:
        verbose_name_plural = 'statuses'

I would like to programatically learn that statuses is a generic relation for the Service model. Is this possible? Status._meta.fields does not show statuses, but Status._meta.get_all_field_names() does, only it shows other unwanted things too.

I thought that this might be a possible solution, but it seems really messy to me. I’d love to hear of a better one.

from django.db.models.fields import FieldDoesNotExist
from django.contrib.contenttypes import generic

generic_relations = []
for field_name in Service._meta.get_all_field_names():
    try:
        field = Service._meta.get_field(field_name)
    except FieldDoesNotExist:
        continue

    if isinstance(field, generic.GenericRelation):
        generic_relations.append(field)

Thank you!

  • 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-06-04T06:56:57+00:00Added an answer on June 4, 2026 at 6:56 am

    The GenericRelation works similarly as ManyToManyField. You could find it in Service._meta.many_to_many:

    filter(lambda f:isinstance(f, generic.GenericRelation), Service._meta.many_to_many)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Would like to get a list of all packages available on CRAN to the
Solved: Was missing view parameter for postData(), changed to reflect this. I would like
I would like a regex string to reflect an optional parameter, in this case,
I would like to reflect the XML tree in my object structure, but I
I have a java.lang.reflect.Method object and I would like to know if it's return
i would like to launch an applet in an build.xml like this: <java classname=sun.applet.AppletViewer
I have a model that is rigged and skinned and I would like to
I would like to do something like this: [HttpPost] public JsonResult Submit(Person UpdatedPerson) {
I would like to get the calling method java.lang.reflect.Method . NOT the name of
Would like to sort the following XML by joining the fsxml/formula/ingrrow where itemcode =

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.