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.
preguntado el 26 de agosto de 12 a las 12:08
Puedes usar 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
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')