18

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.

2 Answers 2

20

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.

6
  • Thanks! It looks like I might have been getting the answer I needed before, I just didn't realize it. Are ContentType ids typically strings? Each number is followed by an 'L' -- content_type_id for Letter is "43L" for Assignment is "44L".
    – Jean Bauer
    Oct 3, 2012 at 23:34
  • So I still don't know what is up with the 'L', but I just did what I should have done in the first place and went into the underlying database to look at the table. Sure enough, the numbers I was getting are correct but the L is nowhere to be found. Weird. But many, many thanks!
    – Jean Bauer
    Oct 3, 2012 at 23:44
  • The L is a very annoying Django thing-- the id type is "long int" in the db, so in some circumstances it comes out with the L appended. If you cast it to an int (int(44L)), you should get 44.
    – Colleen
    Oct 3, 2012 at 23:55
  • Ah. All is now clear. Thanks for taking the time to explain!
    – Jean Bauer
    Oct 4, 2012 at 2:31
  • "L" is appended for all primary keys and foreign keys by default with MySQL due to the use of long int, so that's a guess. Django is usually good at coercion in queryset evaluation though, so you don't often have to worry about it.
    – trojjer
    Dec 10, 2013 at 23:43
10

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'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.