I'm using FactoryBoy and Faker to generate some models for unit tests. Generating data for fields is easy enough, but how to I generate a string that incorporates a value produced from a Faker provider?
import factory
import MyModel
class MyFactory(factory.django.DjangoModelFactory):
class Meta:
model = MyModel
# my_ip will be a temporary variable that is not returned by the factory.
exclude = (my_ip,)
my_ip = factory.Faker("ipv4_private")
my_string = f"String with IP address [{my_ip}]"
Using MyFactory then creates objects with my_string
set to something like:
String with IP address [<factory.faker.Faker object at 0x7fc656aa6358>]
How can I get my_string
to contain something like:
String with the IP address [192.168.23.112]
How do I resolve this to a value, rather than just getting the object? I've tried wrapping in str() with no luck. Do I need to use LazyAttribute or LazyFunction or something from FactoryBoy?