嗨,我尝试将模型导入测试文件时遇到奇怪的问题原因,我收到错误消息:
ModuleNotFoundError:没有名为“ Book”的模块
模块存在是因为我已经从另一个文件复制并粘贴了它。
我的模型代码:
from django.db import models
from isbn_field import ISBNField
from Book.validators import page_validator, date_validator
class Book(models.Model):
title = models.CharField(max_length=100)
publication_date = models.CharField(validators=[date_validator,], max_length=10)
authors = models.ManyToManyField("Author", related_name="author")
ISBN = ISBNField() #validators=[book_unique_validator,]
pages = models.IntegerField(validators=[page_validator,], blank=True)
language = models.CharField(max_length=4)
def __str__(self):
return self.title
def get_all_authors(self):
return "".join([x.name.title() +", " for x in self.authors.all()])[:-2]
class Meta:
ordering = ["title"]
我的测试代码:
from django.test import TestCase
from Book.models import Book, Author
class BookAPITestCase(TestCase):
def create(self):
author_obj = Author.create(
name="test author"
)
book_obj = Book.create(
title="Test_title",
publication_date="2019-11-20",
authors=author_obj,
ISBN="9788381107419",
pages=295,
language="en"
)
def test_author(self):
author_count = Author.objects.count()
self.assertEqual(author_count, 1)
Ps。我尝试了几种不同的导入方式,例如: 来自Book.models 来自模型 来自.models 他们都没有在这个文件中工作。