将生成器传递给 django-autofixture 中的 field_values
参考 django-autofixture 文档,我编写了以下代码(编辑为简洁):
def random_state_generator(self, states=None):
import random
if not states:
states = "waiting|email|post|einv".split('|')
while 1:
yield random.choice(states)
……
rsg = self.random_state_generator()
self.create_test_foo(10, values={'state': rsg})
好吧
field_values = dict(field_values.items() + values.items())
foo_fixture = AutoFixture(FooClass, overwrite_defaults=True,
generate_fk=generate_fk_values,
field_values=field_values)
bunch_of_stuff = foo_fixture.create(foo_count)
, 现在,当我设置好数据库后,我会过滤自己的内容列表, 并
for foo in bunch_of_stuff: print (foo.state)
没有得到预期的随机状态字符串,而是得到以下输出:
生成器对象 random_state_generator 位于 0x242c640>
根据文档,这不是我所期望的,特别是
field_values:以模型的字段名作为键的字典。值可以是分配给字段的静态值、动态生成值的 Generator 实例或不带参数并返回所需值的可调用实例。
我做错了什么?
Referring to django-autofixture documentation, I wrote the following code (edited for terseness):
def random_state_generator(self, states=None):
import random
if not states:
states = "waiting|email|post|einv".split('|')
while 1:
yield random.choice(states)
...
rsg = self.random_state_generator()
self.create_test_foo(10, values={'state': rsg})
...
field_values = dict(field_values.items() + values.items())
foo_fixture = AutoFixture(FooClass, overwrite_defaults=True,
generate_fk=generate_fk_values,
field_values=field_values)
bunch_of_stuff = foo_fixture.create(foo_count)
Okay,
now when I have the DB all set up, and I filter myself a list of stuff,
and go
for foo in bunch_of_stuff: print (foo.state)
instead of getting the expected random state string, I get the following output:
generator object random_state_generator at 0x242c640>
which is not what I'd expect based on the documentation, specifically
field_values: A dictionary with field names of model as keys. Values may be static values that are assigned to the field, a Generator instance that generates a value on the fly or a callable which takes no arguments and returns the wanted value.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,field_values 从 autofixture.generators 中获取一个类实例,所以我所要做的就是:
生成状态列表;
误解是由于将 autofixture 的生成器与通用 python 生成器混淆而引起的。
Okay, the field_values takes a class instance from autofixture.generators, so all I had to do was:
generate the list of states;
the misunderstanding rose from confusing autofixture's generators with generic python generators.
如果您只想要一些原始数据,您可以使用:
def create_doctor():
如果 Doctor.objects.all().count() == 0:
随机导入
avail_item = ['哈特', '肾', '肺', '耳朵', '骨头']
对于范围(10)内的 i:
夹具 = AutoFixture(医生,
field_values={'user_name': 'GauravTyagi', 'speciality': random.choice(avail_item)})
夹具.创建(1)
别的:
print '医生存在于表中......)))))))))))'
In case when u just want some raw data you can use :
def create_doctor():
if Doctor.objects.all().count() == 0:
import random
avail_item = ['Hart', 'Kidney', 'Lungs', 'Ear', 'Bones']
for i in range(10):
fixture = AutoFixture(Doctor,
field_values={'user_name': 'GauravTyagi', 'speciality': random.choice(avail_item)})
fixture.create(1)
else:
print 'Doctors exist in table .........))))))))))))'