Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: how to trigger creation of ContentTypes for new models?

Tags:

django

At what point are new ContentTypes created, and how can I trigger this myself?

I have a bunch of migrations, creating new models and such. After that's all done there is a final data migration that assigns the permissions for these new objects to any groups that already have related permissions. (Hereby 'Related permissions' are chosen by me to make sense within the application)

However, the ContentTypes for the newly created models are not available yet when I run that datamigration. They are when I run all other migrations first, and only then run the extra datamigration. I.e. run the command twice. But making that manual patch is not feasable scenario in my case.

So I'd like to manually trigger the creation of ContentTypes for any new models right before I run my datamigration. How is this done?

like image 838
Klaas van Schelven Avatar asked Nov 02 '11 11:11

Klaas van Schelven


1 Answers

You should use ContentType.objects.get_for_model(), which creates the ContentType instances on demand (it adds some caching as well).

like image 135
Tobu Avatar answered Nov 16 '22 06:11

Tobu