Why am I getting this error "Operationalerror at /admin/core/comment"?

Asked

Viewed 119 times

-2

When I try to access the Commnets field in my admin area I get this error. Why?

OperationalError at /admin/core/comment/

no such column: core_comment.content

Request Method:     GET
Request URL:    http://localhost:8000/admin/core/comment/
Django Version:     3.1.2
Exception Type:     OperationalError
Exception Value:    

no such column: core_comment.content

Exception Location:     /home/danilo/www/github/blog_application/.venv/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py, line 413, in execute
Python Executable:  /home/danilo/www/github/blog_application/.venv/bin/python
Python Version:     3.7.3
Python Path:    

['/home/danilo/www/github/blog_application',
 '/usr/lib/python37.zip',
 '/usr/lib/python3.7',
 '/usr/lib/python3.7/lib-dynload',
 '/home/danilo/www/github/blog_application/.venv/lib/python3.7/site-packages']

Server time:    Wed, 28 Oct 2020 11:14:47 -0300

This is my model:

from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from django.urls import reverse


class Post(models.Model):
    STATUS_CHOICES = (('draft', 'Draft'), ('published', 'Published'))

    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250, unique_for_date='published')
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts')
    content = models.TextField()

    published = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')

    class Meta:
        ordering = ('-published',)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('core:post', args=[self.slug])


class Comment(models.Model):
    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')

    name = models.CharField(max_length=64)
    email = models.EmailField()
    content = models.TextField()

    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    active = models.BooleanField(default=True)

    class Meta:
        ordering = ('created',)

    def __str__(self):
        return f'Comment by {self.name} on {self.post}'

and my admin is this

from django.contrib import admin
from .models import Post, Comment


@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    list_display = ('title', 'slug', 'author', 'published', 'status')
    list_filter = ('status', 'created', 'published', 'author')
    search_fields = ('title', 'content')
    prepopulated_fields = {'slug': ('title',)}
    raw_id_fields = ('author',)
    date_hierarchy = 'published'
    ordering = ('status', 'published')


@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
    list_display = ('name', 'email', 'post', 'created', 'active')
    list_filter = ('active', 'created', 'updated')
    search_fields = ('name', 'email', 'content')
    # search_fields = ('name', 'email')

1 answer

0

The app is trying to access a column of your database that doesn’t exist, this error occurs because you need to run the migrations after any changes to your app’s models.py file.

Execute these commands:

python manage.py makemigrations

Afterward

python manage.py migrate

Browser other questions tagged

You are not signed in. Login or sign up in order to post.