Skip to content

Instantly share code, notes, and snippets.

@Abiriyi
Created February 29, 2024 15:06
Show Gist options
  • Save Abiriyi/080cfa996ec9268fbd232e55798d6128 to your computer and use it in GitHub Desktop.
Save Abiriyi/080cfa996ec9268fbd232e55798d6128 to your computer and use it in GitHub Desktop.
ERROR
I am getting the following error "File "C:\Users\Casimir ABIRIYI\BlogAndTranslatorApp\blog\models.py", line 12, in Attachment
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='attachments')
^^^^
NameError: name 'Post' is not defined
" if I want to 'makemigrations'. Here sre the codes of my models file "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 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
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'"
@Abiriyi
Copy link
Author

Abiriyi commented Feb 29, 2024

I need assistance on the above, please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment