gpt4 book ai didi

python - 使用 Factory Boy 进行 Django 测试时出错

转载 作者:行者123 更新时间:2023-11-30 23:14:20 26 4
gpt4 key购买 nike

我在 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.

最佳答案

修复非常简单:您的工厂继承自 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()]) 调用。

关于python - 使用 Factory Boy 进行 Django 测试时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28799117/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com