File: management.py

package info (click to toggle)
python-django 0.95.1-1etch2
  • links: PTS
  • area: main
  • in suites: etch
  • size: 6,344 kB
  • ctags: 3,919
  • sloc: python: 24,083; makefile: 14; sql: 6
file content (24 lines) | stat: -rw-r--r-- 846 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""
Creates content types for all installed models.
"""

from django.dispatch import dispatcher
from django.db.models import get_models, signals

def create_contenttypes(app, created_models):
    from django.contrib.contenttypes.models import ContentType
    app_models = get_models(app)
    if not app_models:
        return
    for klass in app_models:
        opts = klass._meta
        try:
            ContentType.objects.get(app_label=opts.app_label,
                model=opts.object_name.lower())
        except ContentType.DoesNotExist:
            ct = ContentType(name=str(opts.verbose_name),
                app_label=opts.app_label, model=opts.object_name.lower())
            ct.save()
            print "Adding content type '%s | %s'" % (ct.app_label, ct.model)

dispatcher.connect(create_contenttypes, signal=signals.post_syncdb)