vendredi 23 octobre 2020

Post class does not have objects variable in Django for Beginners

In W S Vincent's "Django for Beginners," on page 71 he creates a simple message board Django project and then creates some testing code. His simple Post class is

from django.db import models

class Post(models.Model):
    text = models.TextField()

    def __str__(self):
        return self.text[:50]

and then he creates a tests.py class with the following code:

from django.test import TestCase
from django.urls import reverse #new
from .models import Post

class PostModelTest(TestCase):

    def setup(self):
        Post.objects.create(text='just a test')

    def test_text_content(self):
        post = Post.objects.get(id=1)
        expected_object_name = f'{post.text}'
        self.assertEqual(expected_object_name, 'just a test')

However, as you can see, there is no Posts.objects variable or method, and the test fails. I don't see what he intended to do here, but those with more Django experience may be able to point out what is wrong.

Aucun commentaire:

Enregistrer un commentaire