Django content_type object_id

2k Views Asked by At

I have model call Report:

class Report(models.Model):
    owner = models.ForeignKey(User, related_name='report_owner')
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

How do I find out what the object_id of my models in all my apps are?

1

There are 1 best solutions below

0
On

so I found the answer in the docs.

 from django.contrib.contenttypes.models import ContentType
my_model = ContentType.objects.get(app_label='myapp', model='mymodel')

Then

my_model.id

returns the object_id.