Hi
so i have a django model which is a bit long, but the part we need is:
catalog/models.py
from django.db import models
class Genres(models.Model):
name = models.Charfield(max_length=200)
def str(self):
return self.name
class Book(models.Model):
“”“some data”“”
genres = models.ManyToManyField(Genre)
which seem to work fine on the admin site
BUT
i can’t seem to be able to test it
my test:
from django.test import TestCase
from .models import Books, Genres
class BookTest(TestCase):
def setUp(self):
“”“i also tried with setUpTestData classmethode”“”
self.genres =
Genres.objects.create(name=“drama”)
self.book = Book.objects.create(
"""some data"""
)
self.all_genres = Genres.objects.all()
self.book.genre.set(self.all_genres)
self.book.save()
and then i assert that
self.assertEqual(f"{self.book.genre}", “drama”)
my error:
Traceback (most recent call last):
File “C:\Users\313.Co\Desktop\django_project\catalog\tests.py”, line 65, in test_book_listing
self.assertEqual(f"{self.book.genres}", “drama”)
AssertionError: ‘catalog.Genres.None’ != ‘drama’
- catalog.Genres.None
- drama