Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate Django database

Tags:

I'm developing a Django application which stores user information like their address, phone number, name, etc.

I've worked with PHP's Faker library and the seeder included with Laravel. I had been able to populate the database with fake data but now I'm working with Django.

I'd like to populate my users table with around 200 entries. But, I don't want the entries to be random strings. I want it to be fake data like I can get with Laravel. I don't know how to do it.

What do I need to do to persist fake data?

This is for showing the end user the application with some entries so he can see statistics and other things. The data will need to stay in the database. I tried using unit tests but that deletes the database after the unit test ends.

Thanks!

like image 359
user3186459 Avatar asked Oct 08 '15 19:10

user3186459


People also ask

How use Django load data?

You'll store this data in a fixtures directory inside your app. You can load data by calling manage.py loaddata <fixturename> , where <fixturename> is the name of the fixture file you've created. Each time you run loaddata , the data will be read from the fixture and reloaded into the database.

Can we connect to database Django?

By default, Django works with SQLite, database and allows configuring for other databases as well. Database connectivity requires all the connection details such as database name, user credentials, hostname drive name etc. To connect with MySQL, django. db.

What is fixture in Django?

You must create a directory in your app named fixtures and put your fixtures files there. You can write them in json or xml, one easy way to make them is to create some objects in the admin interface and then run manage.py dumpdata. That would dump the data from the objects you created into fixture files.


2 Answers

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.

like image 89
Dwayne Crooks Avatar answered Oct 20 '22 16:10

Dwayne Crooks


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/

like image 45
Paulo Pessoa Avatar answered Oct 20 '22 17:10

Paulo Pessoa