19👍
✅
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.
9👍
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'
- How do I get django runserver to show me DeprecationWarnings and other useful messages?
- Pycharm Django Debugging is really slow
- Is there a command for creating an app using cookiecutter-django?
- How can I use AWS's Dynamo Db with Django?
- Django admin.TabularInline auto insert three blank row
Source:stackexchange.com