python - How to call code within Django when new Models are registered -


i'd able trigger code run when new db model registered in django installed_apps.

the use case: want add dynamic global permission on django models in app.

i have code works, have schedule it, , i'd rather have run when new apps registered once.

from django.db import integrityerror django.contrib.auth.models import contenttype, permission  perms_map = {     'get': ['%(app_label)s.view_%(model_name)s'],     'options': ['%(app_label)s.view_%(model_name)s'],     'head': ['%(app_label)s.view_%(model_name)s'],     'post': ['%(app_label)s.add_%(model_name)s'],     'put': ['%(app_label)s.change_%(model_name)s'],     'patch': ['%(app_label)s.change_%(model_name)s'],     'delete': ['%(app_label)s.delete_%(model_name)s'], }  def _create_model_view_permissions():     '''     create 'view_(model_name)' permission models.      run when adding new model db.     '''     ct in contenttype.objects.all():          name = 'can view {}'.format(ct.name)         codename = 'view_{}'.format(ct.name)          # create single instance used in 3 view types         in perms_map.keys():             if in ['head', 'options', 'get']:                 try:                     permission.objects.create(name=name, codename=codename, content_type=ct)                 except integrityerror:                     pass 

new apps/models registered when django server process reloaded or restarted, called deploy.

so, why not write django custom command calls _create_model_view_permissions function first, append running command step deploy script?


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -