Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ContentType id in Django for generic relation

Tags:

django

I'm porting a project from Rails to Django with a legacy database. In Rails I had a polymorphic association that allowed me to add a footnote to any row in the database. I'm trying to implement the same thing in the Django app. I found the documentation on Generic Relations and it looks perfect. Unfortunately, I first need to create new fields in my legacy database to hold the ContentType id for the relevant models. I only used the polymorphic association with 2 tables, so all I need are those two corresponding ids from the Django app, but I can't seem to find the appropriate command for looking up a ContentType id in Django.

Any suggestions are most welcome. I tried searching through previous questions but couldn't seem to find what I am looking for. Thank you very much for you time and help.

like image 940
Jean Bauer Avatar asked Oct 03 '12 21:10

Jean Bauer


People also ask

What is Contenttype in Django?

Django includes a contenttypes application that can track all of the models installed in your Django-powered project, providing a high-level, generic interface for working with your models.

What is GenericForeignKey Django?

In Django, a GenericForeignKey is a feature that allows a model to be related to any other model in the system, as opposed to a ForeignKey which is related to a specific one.

What is Django proxy model?

A proxy model is a subclass of a database-table defining model. Typically creating a subclass of a model results in a new database table with a reference back to the original model's table - multi-table inheritance. A proxy model doesn't get its own database table. Instead it operates on the original table.

What is a model Django?

Django web applications access and manage data through Python objects referred to as models. Models define the structure of stored data, including the field types and possibly also their maximum size, default values, selection list options, help text for documentation, label text for forms, etc.


2 Answers

from the docs

you can do:

>>> b = Bookmark.objects.get(url='https://www.djangoproject.com/')
>>> bookmark_type = ContentType.objects.get_for_model(b)
>>> TaggedItem.objects.filter(content_type__pk=bookmark_type.id,
...                           object_id=b.id)

so just instantiate an instance of your model and then do ContentType.objects.get_for_model(<instance>).id

I think there's a way to pass just the model name, too... let me know if that would work better and I'll try to find it; I've used it in the past.

like image 142
Colleen Avatar answered Oct 19 '22 05:10

Colleen


You can also get ContentType ID without creating an instance, which is more efficient if you don't have and don't need an instance but just want to get ContentType ID by model name.

ContentType.objects.get(model='bookmark').id

Notes : if the name of your model is upper case, please use lower case to search. For example: model = 'bookmark' rather than 'Bookmark'

like image 21
horbor Avatar answered Oct 19 '22 04:10

horbor