gpt4 book ai didi

python - factory_boy : add several dependent objects

转载 作者:太空狗 更新时间:2023-10-30 02:46:13 27 4
gpt4 key购买 nike

我正在使用 factory_boy 替换 Django 应用程序中的固定装置。我有一个产品模型,应该有很多优惠和商家。

#models.py
class Product(models.Model):
name = models.CharField()

class Merchant(models.Model):
product = models.ForeignKey(Product)
name = models.CharField()

class Offer(models.Model):
product = models.ForeignKey(Product)
price = models.DecimalField(max_digits=10, decimal_places=2)

我想要一个工厂来创建具有多个商家和多个报价的产品。

#factories.py
import random
from models import Offer, Merchant, Product

class OfferFactory(factory.django.DjangoModelFactory):
FACTORY_FOR = Offer

product = factory.SubFactory(ProductFactory)
price = random.randrange(0, 50000, 1)/100.0


class MerchantFactory(factory.django.DjangoModelFactory):
FACTORY_FOR = Merchant

product = factory.SubFactory(ProductFactory)
name = factory.Sequence(lambda n: 'Merchant %s' % n)
url = factory.sequence(lambda n: 'www.merchant{n}.com'.format(n=n))

class ProductFactory(factory.django.DjangoModelFactory):
FACTORY_FOR = Product

name = "test product"
offer = factory.RelatedFactory(OfferFactory, 'product')
offer = factory.RelatedFactory(OfferFactory, 'product') # add a second offer
offer = factory.RelatedFactory(OfferFactory, 'product') # add a third offer
merchant = factory.RelatedFactory(MerchantFactory, 'product')
merchant = factory.RelatedFactory(MerchantFactory, 'product') # add a second merchant
merchant = factory.RelatedFactory(MerchantFactory, 'product') # add a third merchant

但是当我使用 ProductFactory 创建产品时,它只有一个报价和一个商家。

In [1]: from myapp.products.factories import ProductFactory

In [2]: p = ProductFactory()

In [3]: p.offer_set.all()
Out[3]: [<Offer: $39.11>]

我如何设置 ProductFactory 以具有多个特定类型的依赖项?

最佳答案

为了能够指定父工厂中相关对象的数量:

模型.py

class Company(models.Model):
name = models.CharField(max_length=255)


class ContactPerson(models.Model):
name = models.CharField(max_length=255)
company = models.ForeignKey(Company, on_delete=CASCADE, related_name='contacts')

工厂.py

class CompanyFactory(factory.django.DjangoModelFactory):
name = factory.Faker('company')

class Meta:
model = Company

@factory.post_generation
def add_contacts(self, create, how_many, **kwargs):
# this method will be called twice, first time how_many will take the value passed
# in factory call (e.g., add_contacts=3), second time it will be None
# (see factory.declarations.PostGeneration#call to understand how how_many is populated)
# ContactPersonFactory is therefore called +1 times but somehow we get right amount of objs
at_least = 1
if not create:
return
for n in range(how_many or at_least):
ContactPersonFactory(contact=self)



class ContactPersonFactory(factory.django.DjangoModelFactory):
name = factory.Faker('first_name')

class Meta:
model = ContactPerson

测试.py

company = CompanyFactory(company_name='ACME ltd', add_contacts=4)
print(repr(company.name), len(company.contacts.all()))
company = CompanyFactory(company_name='ACME ltd')
print(repr(company.name), len(company.contacts.all()))

---
'ACME ltd' 4
'ACME ltd' 1

如果您只接受一个 child ,the docs solution运作良好:

模型.py

class CompanyFactory(factory.django.DjangoModelFactory):
name = factory.Faker('company')
whatever_really = factory.RelatedFactory('my_app.factories.ContactPersonFactory', 'contact')

class Meta:
model = Company

注意相关工厂的完整路径。

测试.py

company = CompanyFactory(company_name='ACME ltd')
print(repr(company.name), len(company.contacts.all()))
---
'ACME ltd' 1

使用的版本

$ pip freeze | egrep 'factory|Faker|Django'
Django==2.0.4
factory-boy==2.10.0
Faker==0.8.13
$ python -V
Python 3.6.5

关于python - factory_boy : add several dependent objects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21564878/

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