无法使用 Factory Boy 和 Faker 创建模型的多个实例

Can not create multiple instance of Model using Factory Boy and Faker

我正在尝试使用 django-factory-boy 和 faker 创建多个 django 模型实例。但是我需要批量创建实例(不是单个)。但是我不能使两个属性都对应(货币的codename)。

我有一个 django 模型:

class Currency(models.Model):
    """Currency model"""
    name = models.CharField(max_length=120, null=False,
                            blank=False, unique=True)
    code = models.CharField(max_length=3, null=False, blank=False, unique=True)
    symbol = models.CharField(max_length=5, null=False,
                              blank=False, default='$')

    def __str__(self) -> str:
        return self.code

我有一个工厂

import factory
from apps.Payment.models import Transaction, Currency
from faker import Faker
fake = Faker()


class CurrencyFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Currency

    # code and name get assigned when the class is called hence if we use
    # create_batch(n) we get all n object same
 
    # code, name = fake.currency() 

    code = factory.LazyAttribute(lambda _: fake.currency()[0]) 
    name = factory.LazyAttribute(lambda _: fake.currency()[1]) 
    symbol = '$'

我面临的问题是 codename 得到不同的值并且不匹配。看什么 faker returns.

>>> from faker import Faker
>>> fake = Faker()
>>> fake.currency()
('JPY', 'Japanese yen') 

看到货币名称与货币代码不对应。我还需要使用 CurrencyFactory.create_batch(5).

创建至少 5 到 6 个对象
# mismatch in code and name
NAME                            CODE

Netherlands Antillean guilder   ZAR
Western Krahn language          UGX
Colombian peso                  KHR

我想要的

NAME                            CODE

Indian National Rupee           INR
Japanese yen                    JPY

最好的方法是通过 class Params:

class CurrencyFactory(factory.model.DjangoModelFactory):
  class Meta:
    model = Currency
  class Params:
    currency = factory.Faker("currency")  # (code, name)

  code = factory.LazyAttribute(lambda o: o.currency[0])
  name = factory.LazyAttribute(lambda o: o.currency[1])

想法是:

  • 工厂调用Faker一次,生成currency = (code, name)参数;
  • 然后它将该参数的组件映射到模型的正确字段
  • 由于 currency 被声明为参数,因此不会传递给模型(它会自动添加到 Meta.exclude