Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

In my Django app, I have two lines in models.py that use ContentType.objects.get():

class Comment (Content):
    text = models.CharField(max_length=500)
    proposal = models.ForeignKey("Proposal", null=False, related_name="comments")
    contentType = ContentType.objects.get(app_label="myapp", model="comment")

class Proposal (Content):
    title = models.CharField(max_length=100)
    text = models.TextField()
    contentType = ContentType.objects.get(app_label="myapp", model="proposal")

They throw an error when I run python manage.py syncdb or even python manage.py shell (django.db.utils.ProgrammingError: relation "django_content_type" does not exist). I've tried dropping and recreating the database to no avail. If I comment out these two lines, syncdb works fine (but this obviously breaks the app).

django.contrib.contenttypes is in my INSTALLED_APPS.

Here is the full traceback:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/core/management/base.py", line 284, in execute
    self.validate()
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/core/management/base.py", line 310, in validate
    num_errors = get_validation_errors(s, app)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/core/management/validation.py", line 34, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/loading.py", line 196, in get_app_errors
    self._populate()
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/loading.py", line 75, in _populate
    self.load_app(app_name, True)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/loading.py", line 99, in load_app
    models = import_module('%s.models' % app_name)
  File "/path/to/virtualenv/lib/python3.4/importlib/__init__.py", line 104, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2231, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2203, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1448, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/path/to/django/app/models.py", line 42, in <module>
    class Comment (Content):
  File "/path/to/django/app/models.py", line 46, in Comment
    contentType = ContentType.objects.get(app_label="myapp", model="comment")
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/manager.py", line 151, in get
    return self.get_queryset().get(*args, **kwargs)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/query.py", line 301, in get
    num = len(clone)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/query.py", line 77, in __len__
    self._fetch_all()
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/query.py", line 854, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/query.py", line 220, in iterator
    for row in compiler.results_iter():
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 709, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 782, in execute_sql
    cursor.execute(sql, params)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/backends/util.py", line 69, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/utils/six.py", line 535, in reraise
    raise value.with_traceback(tb)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "django_content_type" does not exist
LINE 1: ..."."app_label", "django_content_type"."model" FROM "django_co...
share|improve this question

I'm not sure what you are trying to do, but you can't use model objects like that in the definition of another model. You are asking Django to get a specific instance of ContentType before it does anything else - before even it has a chance to create the table for ContentType. You need a foreign key to ContentType instead.

class Comment (Content):
    text = models.CharField(max_length=500)
    proposal = models.ForeignKey("Proposal", null=False, related_name="comments")
    contentType = models.ForeignKey(ContentType)

Assuming though that what you are actually after is a generic relationship, you also need to add a field for the object id of whatever class is pointed to by the relationship, plus a pseudo field to make accessing that easier:

    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('contentType', 'object_id')

See the generic relations documentation.

share|improve this answer
    
The goal was to have an easy way to see the type of the Content subclass. I ended up writing a function called get_content_type() that checks if the class has a _content_type attribute, and if not, runs Comment._content_type = ContentType.objects.get(app_label="comments", model="comment"). – hecka_tao Sep 21 '14 at 20:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.