30👍
I know this is an old question but for anyone who might come across this, here’s another approach that you can use.
>>> from factory.faker import faker
>>> FAKE = faker.Faker()
>>> FAKE.name()
'Scott Rodriguez'
>>> FAKE.address()
'PSC 5061, Box 1673\nAPO AP 53007'
>>>
30👍
You can use faker with factory_boy like this:
class RandomUserFactory(factory.Factory):
class Meta:
model = models.User
first_name = factory.Faker('first_name')
user = RandomUserFactory()
print user.first_name
# 'Emily'
So you need to instantiate a user with factory_boy and it will call Faker for you.
I don’t know if you are trying to use this with Django or not,
but if you want the factory to save the created user to the database,
then you need to extend factory.django.DjangoModelFactory instead of factory.Factory.
- [Django]-Django: Catching Integrity Error and showing a customized message using template
- [Django]-How to start doing TDD in a django project?
- [Django]-How do I check for last loop iteration in Django template?
12👍
factory_boy
doesn’t provide a public/documented interface to get to the Faker
instances it uses. So preferably create yourself a separate one. But in case you really need to:
import factory
print(factory.Faker._get_faker().random_int())
print(factory.Faker._get_faker('en_US').random_int())
You can also use generate()
/evaluate()
, but the interface changed with time:
# 3.0.1
# uses self.locale
print(factory.Faker('random_int').generate())
print(factory.Faker('random_int').evaluate(None, None, None))
print(factory.Faker('random_int', locale='en_US').generate())
print(factory.Faker('random_int', locale='en_US').evaluate(None, None, None))
# 3.1.0
# uses locale from the parameters
print(factory.Faker('random_int').generate({'locale': None}))
print(factory.Faker('random_int').evaluate(None, None, {'locale': None}))
print(factory.Faker('random_int').generate({'locale': 'en_US'}))
print(factory.Faker('random_int').evaluate(None, None, {'locale': 'en_US'}))
# 3.2.0, 3.2.1
# no generate()
print(factory.Faker('random_int').evaluate(None, None, {'locale': None}))
print(factory.Faker('random_int').evaluate(None, None, {'locale': 'en_US'}))
You may also check out the other answer for a more detailed example.
- [Django]-Filter foreignkey field in django admin
- [Django]-What is the path that Django uses for locating and loading templates?
- [Django]-Django's SuspiciousOperation Invalid HTTP_HOST header
4👍
First, if you want to use factory_boy
with a Django model, you should use DjangoModelFactory
as it is recommended.
Second, factory_boy
also suggests to use Faker
attribute declaration in order to easily define realistic-looking factories. (see providers)
class RandomUserFactory(factory.DjangoModelFactory):
class Meta:
model = 'myapp.User' # Equivalent to model = myapp.models.User
first_name = factory.Faker('first_name')
Once you have defined your factory, you can simply use it as follows:
>>> o = RandomUserFactory()
>>> o.first_name
Tim
- [Django]-How to debug Django commands in PyCharm
- [Django]-Paginating the results of a Django forms POST request
- [Django]-Django set DateTimeField to database server's current time
3👍
As the other answers have already clarified, the proper/usual way to use Faker is with factories. However, it is occasionally useful to be able to use Faker inline, and that seems to be the underlying question here.
Here is an updated answer for 2022 and latest factoryboy versions:
from typing import Any
from factory import Faker
def get_fake(provider: str, locale: str | None = None) -> Any:
""" e.g. `get_fake('name')` ==> 'Buzz Aldrin' """
if not locale:
locale = Faker._DEFAULT_LOCALE # pylint: disable=protected-access
return Faker(provider).evaluate({}, None, {'locale': locale})
- [Django]-Django: allow line break from textarea input
- [Django]-Django templates and variable attributes
- [Django]-Django: how to do calculation inside the template html page?