Generando datos para el desarrollo en 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.

preguntado el 26 de agosto de 12 a las 12:08

3 Respuestas

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

Respondido 26 ago 12, 12:08

To answer your question directly - such scripts are run as comandos de gestión personalizados, but it would be simpler to use a pre-populated database like Northwind. See esta respuesta on how how to implement it for django.

contestado el 23 de mayo de 17 a las 11:05

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')

Respondido 17 Feb 21, 07:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.