Question

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!

Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top