I am writing a blog application in Django. In the models.py I have two classes: Post and LanguageCategory. In language category I would predefine which languages are applicable such as English, Italian, and this could be created by superuser in admin login. However, inside the Post class I want to use the LanguageCategory class as a property called languages as follows (code for models.py):
from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
# Create your models here.
# https://stackoverflow.com/questions/29536180/django-blog-adding-published-date-field-to-new-posts-and-ability-to-login-as-non
class Post(models.Model):
title = models.CharField(max_length=300)
keywords = models.CharField(max_length=300, default="some keywords here")
author = models.ForeignKey(User, on_delete=models.CASCADE)
created_date = models.DateTimeField(
default=timezone.now)
published_date = models.DateTimeField(auto_now_add=True,
blank=False, null=False)
language = LanguageCategory()
image = models.ImageField(upload_to='blog_images/', default='blog_images/myimage.png')
body = models.TextField()
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.title + ' | ' + str(self.author)
class LanguageCategory(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
When I try to make migrations I get the following error:
line 15, in Post language = LanguageCategory() NameError: name 'LanguageCategory' is not defined
How to fix this issue and how can I use LanguageCategory in the Post such that the blog posts has a field which selects the languages?