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')),
],
),
python manage.py showmigrations <yourapp>
, and any migrations files for your app that containsComment
. – Alasdair