Videos
You can use python providers as well.
class MyModel(factory.django.DjangoModelFactory)
number_field = factory.Faker('pyint', min_value=0, max_value=1000)
class Meta:
model = SomeModel
Documentation
I was able to figure out this problem by using a lazy attribute (factory.LazyAttribute). From the docs:
Most factory attributes can be added using static values that are evaluated when the factory is defined, but some attributes (such as fields whose value is computed from other elements) will need values assigned each time an instance is generated.
class FabricFactory(DjangoModelFactory):
class Meta:
model = Fabric
title = factory.Faker('name')
description = factory.Faker('catch_phrase')
price = factory.LazyAttribute(random.randrange(MIN_PRICE, MAX_PRICE + 1))
What's "a random decimal"? Decimals have arbitrary precision, so generating a number with as much randomness as you can hold in a Decimal would take the entire memory of your machine to store.
You have to know how many decimal digits of precision you want in your random number, at which point it's easy to just grab an random integer and divide it. For example if you want two digits above the point and two digits in the fraction (see randrange here):
decimal.Decimal(random.randrange(10000))/100
From the standard library reference :
To create a Decimal from a float, first convert it to a string. This serves as an explicit reminder of the details of the conversion (including representation error).
>>> import random, decimal
>>> decimal.Decimal(str(random.random()))
Decimal('0.467474014342')
Is this what you mean? It doesn't seem like a pita to me. You can scale it into whatever range and precision you want.
» pip install Faker