0
votes

I'm trying to create a Comment class so that I can comment any type of object with it. I have found a solution here : https://docs.djangoproject.com/en/1.11/ref/contrib/contenttypes/ However, it didn't work. (I must admit that i didn't understand everything about the content_type and object_id they used, I just copied the code from their page)

Here is my model :

class Comment(models.Model):
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')
    author = models.CharField(max_length=200, default="userPasDef")
    text = models.TextField()
    created_date = models.DateTimeField(default=timezone.now)

    def publish(self):
        self.created_date = timezone.now()
        self.author = "userPasDef"
        self.save()

    def __str__(self):
        return self.text

Here is the error :

Traceback (most recent call last): File "/home/computer/Documents/virtualenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute return self.cursor.execute(sql, params) psycopg2.ProgrammingError: column "content_type_id" of relation "Application_comment" does not exist LINE 1: INSERT INTO "Application_comment" ("content_type_id", "objec...

But I don't have any "content_type_id" in my model... I can run makemigrations, migrate and runserver, but each time I try to post a comment on my site this error appears. What could have caused this problem ?

Thanks ! :)


Edit : "Show the output of python manage.py showmigrations "

Application [X] 0001_initial

(I tried deleting all my database and previous migrations to reset them but it didn't solve the problem)

"and any migrations files for your app that contains Comment"

        migrations.CreateModel(
        name='Comment',
        fields=[
            ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ('object_id', models.PositiveIntegerField()),
            ('author', models.CharField(default='userPasDef', max_length=200)),
            ('text', models.TextField()),
            ('created_date', models.DateTimeField(default=django.utils.timezone.now)),
            ('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType')),
        ],
    ),
1
are you sure that you don't have pending migrations?alfonso.kim
Show the output of python manage.py showmigrations <yourapp>, and any migrations files for your app that contains Comment.Alasdair
Edited my post :) And yes, I'm sure I don't have pending migrations !PyLily
"ContentType" is your model or you take it from the django default? Because if you che migrations, there is an app called "contenttypes" and maybe can create confictMyth
I took ContentType from the django defaults (from django.contrib.contenttypes.models import ContentType) :)PyLily

1 Answers

1
votes

So, for anyone who would have the same problem as me, the problem was not in my models but in my database. I dropped all the tables in postgre, made my migrations again and everything was working. I really shouldn't have deleted all these migration files (I didn't know they were so important). Django is lost without them because he can't remember the current state of the database. :)