gpt4 book ai didi

python - 如何为Factory Boy指定数据库?

转载 作者:太空狗 更新时间:2023-10-30 00:06:59 24 4
gpt4 key购买 nike

FactoryBoy 似乎总是在默认数据库中创建实例。但是我有以下问题。

cpses = CanonPerson.objects.filter(persons__vpd=6,
persons__country="United States").using("global")

代码指向global 数据库。我还没有找到在工厂内指定数据库的方法:

class CanonPersonFactory(django_factory.DjangoModelFactory):
class Meta:
model = CanonPerson
django_get_or_create = ('name_first', 'p_id')
p_id = 1
name_first = factory.Sequence(lambda n: "name_first #%s" % n)

@factory.post_generation
def persons(self, create, extracted, **kwargs):
if not create:
# Simple build, do nothing.
return

if extracted:
# A list of groups were passed in, use them
for person in extracted:
self.persons.add(person)

最佳答案

看起来 Factory Boy 没有提供这个功能,但是你可以很容易地手动添加它:

class CanonPersonFactory(django_factory.DjangoModelFactory):
class Meta:
model = CanonPerson
...
@classmethod
def _get_manager(cls, model_class):
manager = super(CanonPersonFactory, cls)._get_manager(model_class)
return manager.using('global')
...

关于python - 如何为Factory Boy指定数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26550262/

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