Django FactoryBoy:用选择填充modelfield会引发错误 [英] Django FactoryBoy: fill modelfield with choices throws error

查看:114
本文介绍了Django FactoryBoy:用选择填充modelfield会引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为模型厂工作,我正在尝试填写具有选择列表的字段.当我尝试使用Factory创建一个对象,并尝试从选择列表中填充随机选择时,会引发异常:

I am working on a factory for a model and I am trying fill a field that has a list of choices. When I attempt to create an object with the Factory where I attempt to fill in a random choice from the choice list, an exception is thrown:

TypeError:"choice"是此函数的无效关键字参数

TypeError: 'choice' is an invalid keyword argument for this function

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/factory/base.py", line 551, in build
    return cls._generate(enums.BUILD_STRATEGY, kwargs)
  File "/usr/local/lib/python2.7/dist-packages/factory/base.py", line 505, in _generate
    return step.build()
  File "/usr/local/lib/python2.7/dist-packages/factory/builder.py", line 279, in build
    kwargs=kwargs,
  File "/usr/local/lib/python2.7/dist-packages/factory/base.py", line 312, in instantiate
    return self.factory._build(model, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/factory/base.py", line 531, in _build
    return model_class(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 571, in __init__
    raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
TypeError: 'choice' is an invalid keyword argument for this function

使用的版本:

django==1.11
factory-boy==2.9.2
python==2.7.12

(裁剪后的)模型:

class Server(models.Model):

    TEST = 'test'
    ACCEPT = 'accept'

    SERVER_TYPES = (
        (TEST, _("Testing Server")),
        (ACCEPT, _("Acceptation Server"))
    )

    type = models.CharField(_("Server type"), max_length=50, choices=SERVER_TYPES)

(裁剪后的)工厂:

class ServerFactory(factory.DjangoModelFactory):

    type = factory.Faker('random_element', elements=[choice[0] for choice in Server.SERVER_TYPES)

    class Meta:
        model = Server

除了使用Faker('random_element, elements=[..])之外,我还尝试使用LazyFunction:

In stead of using Faker('random_element, elements=[..]), I've also tried using the LazyFunction:

def get_server_type():
    choices = [choice[0] for choice in Server.SERVER_TYPES]
    return random.choice(choices)

class ServerFactory(factory.DjangoModelFactory):

    organization = factory.SubFactory(OrganizationFactory)
    type = factory.LazyFunction(get_server_type)

    .. Meta ..

这也会引发相同的错误.我也找不到其他真正的替代方法来解决此问题.关于使用factory软件包时如何用SERVER_TYPES选项之一填充type字段的任何建议?

This also throws the same error. I also cannot find any real other alternatives to fix this. Any suggestions how I can fill the type field with one of the SERVER_TYPES choices while using the factory package?

推荐答案

您可以试试吗?

from random import choice
type = factory.LazyAttribute(lambda x: choice(Server.SERVER_TYPES)[0])

基于问题的初始描述的旧评论:

Old comment based on initial description of the question:

应为type = factory.Faker('random_element', elements=[choice[0] for choice in Server.SERVER_TYPES])

这篇关于Django FactoryBoy:用选择填充modelfield会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆