pip install Faker
How to create a Provider
How to create a Provider
from faker import Faker
fake = Faker()
# first, import a similar Provider or use the default one
from faker.providers import BaseProvider
# create new provider class
class MyProvider(BaseProvider):
def foo(self):
return 'bar'
# then add new provider to faker instance
fake.add_provider(MyProvider)
# now you can use:
fake.foo()
> 'bar'
>>> MyProvider.foo(1)
'bar'
import factory
from myapp.models import Book
class BookFactory(factory.Factory):
class Meta:
model = Book
title = factory.Faker('sentence', nb_words=4)
author_name = factory.Faker('name')
#这个我没有实现
参考:https://pypi.org/project/Faker/0.7.8/