1

我在我的 Django 项目中使用 Factory Boy。您能否向我解释一下我的错误:为什么我在运行“tests.py”时出现错误 - 'ValueError:“”需要字段“post”的值才能使用这种多对多关系。

这是我的代码:

import factory

from . import models

# factories


class TagFactory(factory.Factory):
    class Meta:
        model = models.Tag

    name = factory.Sequence(lambda n: 'tag-%s' % n)
    slug = factory.Sequence(lambda n: 'slug-%s' % n)


class CategoryFactory(factory.Factory):
    class Meta:
        model = models.Category

    name = factory.Sequence(lambda n: 'cat-%s' % n)
    slug = factory.Sequence(lambda n: 'cat-slug-%s' % n)


class PostFactory(factory.Factory):
    class Meta:
        model = models.Post

    title = factory.Sequence(lambda n: 'postik-%s' % n)
    text = 'some text'
    slug = factory.Sequence(lambda n: 'post_slug_%s' % n)
    category = factory.SubFactory(CategoryFactory)

    @factory.post_generation
    def tags(self, create, extracted, **kwargs):
        if not create:
            # Simple build, do nothing.
            return
        if extracted:
            for tag in extracted:
                self.tags.add(tag)

测试.py

from django.test import TestCase

from . import factories
from .models import Post, Category, Tag
from django.core.urlresolvers import reverse


class PostTests(TestCase):
    """
    display_post view test.
    """
    def setUp(self):
        self.tag = factories.TagFactory()
        self.category = factories.CategoryFactory()
        self.poster = factories.PostFactory.create(tags=('tag-1'))

 ...

模型.py

from django.db import models
from django.core.urlresolvers import reverse
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _

from ckeditor.fields import RichTextField


class Category(models.Model):
    """
    Category.
    """

    name = models.CharField(max_length=20)
    slug = models.SlugField(max_length=20)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('categorier', args=[str(self.slug)])


class Tag(models.Model):
    """
    Tag.
    """

    name = models.CharField(max_length=100, unique=True)
    slug = models.SlugField(max_length=100, unique=True)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('tagger', args=[str(self.slug)])

    class Meta:
        verbose_name = _('Tag')


class Post(models.Model):
    """
    Post model.
    """

    title = models.CharField(max_length=150)
    created_date = models.DateTimeField(auto_now_add=True)
    image = models.ImageField(upload_to="pictures/%Y/%m/%d",
                                        blank=True, null=True)
    text = RichTextField(max_length=10000)
    slug = models.SlugField(max_length=150, unique=True)
    tags = models.ManyToManyField(Tag, null=True, blank=True)
    category = models.ForeignKey(Category)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('poster', args=[str(self.slug)])

    class Meta:
        ordering = ['-created_date']
        verbose_name = _('Eco Post')
        verbose_name_plural = _('Eco Posts')

追溯:

ERROR: test_display_post (posts.tests.PostTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/boshepelinski/Documents/Projects/Django/ecohata/ecohata/posts/tests.py", line 17, in setUp
    self.poster = factories.PostFactory.create(tags=('tag-1'))
  File "/Users/boshepelinski/Documents/Projects/Django/ecohata/lib/python3.4/site-packages/factory/base.py", line 585, in create
    return cls._generate(True, attrs)
  File "/Users/boshepelinski/Documents/Projects/Django/ecohata/lib/python3.4/site-packages/factory/base.py", line 516, in _generate
    results[name] = decl.call(obj, create, extraction_context)
  File "/Users/boshepelinski/Documents/Projects/Django/ecohata/lib/python3.4/site-packages/factory/declarations.py", line 490, in call
    extraction_context.value, **extraction_context.extra)
  File "/Users/boshepelinski/Documents/Projects/Django/ecohata/ecohata/posts/factories.py", line 41, in tags
    self.tags.add(tag)
  File "/Users/boshepelinski/Documents/Projects/Django/ecohata/lib/python3.4/site-packages/django/db/models/fields/related.py", line 1175, in __get__
    through=self.field.rel.through,
  File "/Users/boshepelinski/Documents/Projects/Django/ecohata/lib/python3.4/site-packages/django/db/models/fields/related.py", line 831, in __init__
    (instance, source_field_name))
ValueError: "<Post: postik-0>" needs to have a value for field "post" before this many-to-many relationship can be used.
4

1 回答 1

3

修复非常简单:您的工厂继承自factory.Factory,但您使用的是 Django 模型。你应该继承factory.django.DjangoModelFactory

否则,一旦生成,factory_boy就不知道它对save()你的对象有影响,然后你就会失败。

您的代码中还有另一个问题:当您编写时factories.PostFactory.create(tags=('tag-1')),这实际上与编写相同factories.PostFactory.create(tags='tag-1)

然而:

  • 您的标签声明的定义@post_generation需要一个标签列表,因此您应该使用factories.PostFactory.create(tags=['tag-1'])
  • 实际上,由于它调用self.tags.add(tag),它需要一个可迭代的Tag对象;应该通过调用它factories.PostFactory.create(tags=[TagFactory()])
于 2015-03-02T09:06:23.053 回答