| 1 | """ |
| 2 | Creates content types for all installed models. |
| 3 | """ |
| 4 | |
| 5 | from django.dispatch import dispatcher |
| 6 | from django.db.models import get_models, signals |
| 7 | |
| 8 | def create_contenttypes(app, created_models): |
| 9 | from django.contrib.contenttypes.models import ContentType |
| 10 | app_models = get_models(app) |
| 11 | if not app_models: |
| 12 | return |
| 13 | for klass in app_models: |
| 14 | opts = klass._meta |
| 15 | try: |
| 16 | ContentType.objects.get(app_label=opts.app_label, |
| 17 | model=opts.object_name.lower()) |
| 18 | except ContentType.DoesNotExist: |
| 19 | ct = ContentType(name=str(opts.verbose_name), |
| 20 | app_label=opts.app_label, model=opts.object_name.lower()) |
| 21 | ct.save() |
| 22 | print "Adding content type '%s | %s'" % (ct.app_label, ct.model) |
| 23 | |
| 24 | dispatcher.connect(create_contenttypes, signal=signals.post_syncdb) |