[Django]-Populate Django database

40👍

To get it done in a nice way you’ll need a combination of Factory Boy, Faker and custom management commands.

Factory Boy allows you to create templates for producing valid objects and Faker generates fake data.

When you install Factory Boy, pip install factory_boy, you also get Faker.

Given,

from django.db import models


class User(models.Model):
    name = models.CharField(max_length=64)
    address = models.CharField(max_length=128)
    phone_number = models.CharField(max_length=32)

You can define a Factory as follows:

import factory  
import factory.django

class UserFactory(factory.django.DjangoModelFactory):  
    class Meta:
        model = User

    name = factory.Faker('name')
    address = factory.Faker('address')
    phone_number = factory.Faker('phone_number')

Then, you can create fake users by calling UserFactory.create().

One way to get your 200 fake users would be to jump into the shell, python manage.py shell, and do:

 >>> # import UserFactory here
 >>> for _ in range(200):
 ...     UserFactory.create()

Another way, which can give you a lot more flexibility, is to create a custom management command.

For example, create seed.py (this will be the management command name) in the directory <yourapp>/management/commands (to have it discovered by Django) with the following:

# <yourapp>/management/commands/seed.py
from django.core.management.base import BaseCommand

# import UserFactory here


class Command(BaseCommand):
    help = 'Seeds the database.'

    def add_arguments(self, parser):
        parser.add_argument('--users',
            default=200,
            type=int,
            help='The number of fake users to create.')

    def handle(self, *args, **options):
        for _ in range(options['users']):
            UserFactory.create()

And, you’d run it via the command-line with python manage.py seed or python manage.py seed --users 50 for example.

2👍

Try the django-autofixture app:
https://github.com/gregmuellegger/django-autofixture

This app aims to provide a simple way of loading masses of randomly
generated test data into your development database. You can use a
management command to load test data through command line.

It is named autofixture because it is based on django’s fixtures.
Without autofixture you add test data through the admin to see how the
non-static pages on your site look. You export data by using dumpdata
to send it to your colleagues or to preserve it before you make a
manage.py reset app and so on. As your site grows in complexity the
process of adding and re-adding data becomes more and more annoying.

See this django packages too, maybe can help with fake tests and others problems.
https://www.djangopackages.com/grids/g/fixtures/

0👍

To generate fake data for django you can use django-seed.
It’s an easy process as

  • pip install django-seed (install django-seed)

  • add django_seed in your apps in settings.py file.

    INSTALLED_APPS = (
        ...
        'django_seed',
    )
    
  • python manage.py seed <app-name>

for example: to seed api app of django python manage.py seed api --number=15

If you need, you can also specify what value a particular field should have. For example, if you want to seed 15 of MyModel, but you need my_field to be the same on all of them, you can do it like this:

python manage.py seed api --number=15 --seeder "MyModel.my_field" "1.1.1.1"

Leave a comment