2

To be able to see how my django application looks like, and performs with lots of data, i would like to programmatically generate data in the database. In the django documentation they propose either using fixtures oder SQL statements, but i would rather use a simple python loop to generate thousends of random entries by using the django model classes.

How can i execute such a script? I am using south for database migration, but even there such generation of data seems not to be supported.

Janosch
  • 1,204
  • 1
  • 11
  • 19

3 Answers3

4

You can use django-whatever (enhanced django-any) - it easily creates dummy data.

Here is my sample (in *app_name*/management/commands/dummyitems.py):

class Command(BaseCommand):
    args = '[count]'

    def handle(self, count=20, *args, **options):

        try:
            i = int(count)
        except ValueError:
            print u'n is to be a number!'
            sys.exit(1)

        for _ in xrange(i):
            # you can pass params explicitly
            m = any_model(MY_MODEL_CLASS, image=None)
            m.save()

And so if I need 100 dummy items I run:

$ python manage.py dummyitems 100
Nikita Hismatov
  • 1,546
  • 1
  • 13
  • 30
2

To answer your question directly - such scripts are run as custom management commands, but it would be simpler to use a pre-populated database like Northwind. See this answer on how how to implement it for django.

Community
  • 1
  • 1
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
0

First Install this package https://pypi.org/project/model-mommy/ and then Run this code on django shell. It generates fake data for all of your models in project.

from django.apps import apps
from django.conf import settings
from model_mommy import mommy


for app in settings.INSTALLED_APPS:
    try:
        app_models = apps.get_app_config(app).get_models()
    except:
        continue
    for model in app_models:
        try:
            mommy.make(model, _quantity=100)
        except:
            print('error')
Ali Soltani
  • 589
  • 1
  • 7
  • 21