Harkirat Saluja
Harkirat Saluja

Reputation: 8114

How is value for content_type_id generated in generic foreign key?

I was trying to use generic foreign key for one of my projects, I went through the django documentation, so its basically combination of content_type and object_id.

Now in my table is see that an integer value is generated for content_type_id, so my question is that how is the integer set for the content_type_id?

Upvotes: 0

Views: 573

Answers (2)

ger.s.brett
ger.s.brett

Reputation: 3308

The number is assigned by the contentype framework. You can take a look at this by adding this to your admin.

Add these lines in any of your apps admin.py file:

from django.contrib.contenttypes.models import ContentType
class CTAdmin(admin.ModelAdmin):
    list_display=('app_label','model', 'id')
admin.site.register(ContentType, CTAdmin) 

Then you'll see a Contenttype app in your admin and this will show a list of all models and ids.

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599778

It's a foreign key to the ContentTypes table.

Upvotes: 1

Related Questions