databasedjangoinitializationdjango-south

Generating data for development in django


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.


Solution

  • 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