I get the following errors when I run makemigrations “File “C:\Users\Casimir ABIRIYI\BlogAndTranslatorApp\blog\models.py”, line 24, in Post
attachments = models.ManyToManyField(Attachment, blank=True, related_name=‘attached_to_posts’)
^^^^^^^^^^
NameError: name ‘Attachment’ is not defined” Here are my modols.py file codes
from django.db import models
from django.utils.text import slugify
from django.contrib.auth.models import User
from django.urls import reverse
import logging
logger = logging.getLogger(__name__)
STATUS = ((0, 'Draft'), (1, 'Published'))
class Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
date_created = models.DateTimeField(auto_now_add=True)
slug = models.SlugField(max_length=200, unique=True)
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='author_posts')
status = models.IntegerField(choices=STATUS, default=1)
owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='owner_posts')
image = models.ImageField(upload_to='post_images/', null=True, blank=True)
pdf = models.FileField(upload_to='post_pdfs/', blank=True, null=True)
local_video = models.FileField(upload_to='post_local_videos/', blank=True, null=True)
video_url = models.URLField(blank=True, null=True)
audio = models.FileField(upload_to='post_audios/', blank=True, null=True)
attachments = models.ManyToManyField(Attachment, blank=True, related_name='attached_to_posts')
def get_absolute_url(self):
return reverse('blog:post_detail', args=[str(self.slug)])
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
logger.debug(f"Generated slug for post '{self.title}': {self.slug}")
super().save(*args, **kwargs)
def __str__(self):
return self.title
class Meta:
ordering = ['-date_created']
verbose_name = 'Post'
verbose_name_plural = 'Posts'
db_table = 'post_table'
class Attachment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='attachments')
file = models.FileField(upload_to='attachments/')
def __str__(self):
return self.file.name