Factory Boy — An alternative for Fixtures

myapp
|- __init__.py
|- admin.py
|- forms.py
|- models.py
|- tests
|- __init__.py
|- factories.py
|- tests.py
|- urls.py
|- views.py
# factories.py
from factory import DjangoModelFactory, lazy_attribute
from django.contrib.auth.models import User
class UserFactory(DjangoModelFactory):
first_name = 'Shirisha'
last_name = 'Gaddi'
username = factory.Sequence(lambda n: 'user_%d' % n)
email = lazy_attribute(lambda obj: obj.username + "@example.com")
class Meta:
model = 'User'
django_get_or_create = ('username',)
# Create a user instance by overriding first_name
user = factories.UserFactory(first_name="Karuna")

The article was originally published at MicroPyramid blog

--

--

--

Python, Django, Android and IOS, reactjs, react-native, AWS, Salesforce consulting & development company

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Internet Computer’s Evolution From Copper to Sodium

Final Project — Update 2

Beta Update: Internal Exchange

Using VBA and Excel to Make Authenticated Requests for Alpaca’s Trading API

How to Discover Your Sap HANA Assets in Google Data Catalog

Installing and Configuring Apache Airflow

Docker: host Wiki.js+ SQLite with persistent data

How to 10x Your Success with an Agile Development Process

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
MicroPyramid

MicroPyramid

Python, Django, Android and IOS, reactjs, react-native, AWS, Salesforce consulting & development company

More from Medium

How to implement dependent dropdown List in django admin

Django Import Export Tricks & Tips

How to create desktop notifications in Django?

When to Use Blank and Null on Django Models