Reputation: 3459
So I have an object like this:
class ContactPhoneFactory(factory.Factory):
class Meta:
model = ContactPhoneNumber
number = Faker().phone_number()
type = factory.Faker('random_element', elements=range(5))
I want to apply a function 'formatted_number' to 'number'
if I apply:
number = formatted_number(Faker().phone_number())
it won't apply the function to the output of Faker, it'll just take the Faker object as input. Lazy attribute causes the same problem. How can I apply the formatting function to the faker object?
Upvotes: 1
Views: 464
Reputation: 7529
I've hit a very similar issue myself (also on the PhoneNumber factory): I just wanted to restrict the numbers faker generates to only mobile numbers.
I just implemented my own faker provider. This has the added benefit of not being tied to factory-boy API changes (faker is still pretty new to factory-boy).
I think it goes something like this:
from faker.providers.phone_number import Provider as PhoneProvider
class MyPhoneProvider(PhoneProvider):
@classmethod
def formatted_phone_number(cls):
return formatted_number(cls.phone_number())
factory.Faker.add_provider(MyPhoneProvider)
number = factory.Faker('formatted_phone_number')
Upvotes: 1