Package: django-generate
Django GenerateDjango slightly smarter than fixtures content generation app.django-generateadds a management command calledgeneratewhich allows you to create objects from a dynamically created JSON description of said objects. It’s primary focus is to generate test content for use during project development and testing. Objects are only created once via Django’sget_or_createmethod. Inheritance base model objects are created where needed. File fields can also be populated from arbitrary resources. In this waydjango-generatesimplifies generating complex objects when compared toDjango’s built in fixtures feature.ContentsDjango GenerateInstallationUsagePraekelt FoundationChangelog0.0.60.0.50.0.40.0.3 (2011-08-12)0.0.2 (2011-07-26)0.0.1This package is part of the largerJmboproject.InstallationInstall or adddjango-generateto your Python path.Addgenerateto yourINSTALLED_APPSsetting.UsageIn order to generate content you need to execute thegeneratemanagement command. This command will search for ageneratormodule in each of the apps as specified in theINSTALLED_APPSsetting and call itsgeneratemethod. This method should return a list of JSON serialized objects to be created.Note: Generation is also triggered after asyncdb, at which time you will be prompted to generate default content. If you answer yes to the prompt content will be generated in the same way is if you had run thegeneratecommand manually.As an example lets create 5 dummy users for testing.Create agenerator.pyin the app you want to generate content’s path.Make sure your app is specified in yourINSTALLED_APPSsetting. Also make sure your app has amodels.pyso Django installs it correctly.Edit thegenerator.pyfile to look like this:def generate():
    objects = []
    for i in range(1, 6):
        objects.append({
            "model": "auth.User",
            "fields": {
                "username": "user_%s" % i,
                "first_name": "User %s Name" % i,
                "is_staff": True,
            },
        })
    return objectsAll this is really doing is generating a bunch of JSON serialized objects dynamically. The returnedobjectslist looks like this:[{'fields': {'username': 'user_1', 'first_name': 'User 1 Name', 'is_staff': True}, 'model': 'auth.User'}, {'fields': {'username': 'user_2', 'first_name': 'User 2 Name', 'is_staff': True}, 'model': 'auth.User'}, {'fields': {'username': 'user_3', 'first_name': 'User 3 Name', 'is_staff': True}, 'model': 'auth.User'}, {'fields': {'username': 'user_4', 'first_name': 'User 4 Name', 'is_staff': True}, 'model': 'auth.User'}, {'fields': {'username': 'user_5', 'first_name': 'User 5 Name', 'is_staff': True}, 'model': 'auth.User'}]This is a normal Django JSON fixtures list of objects that will be created. You could just as easily have hard coded and returned this list instead of generating it. The point is that thegeneratemethod should return a list of JSON serialized objects to be created.Run the generate management command to generate the objects:$ python manage.py generateAfter the command completes you should have 5 newly created staff users in your database. If you were to run the generate command again no new users would be created asdjango-generatedetects the presence of previously generated objects.Have a look atjmbo-post’s generatorto see how objects with inheritance structures, relations and file resources can be created very easily usingdjango-generate.Praekelt FoundationShaun SephtonJonathan BydendykEuan JonkerHedley RoosChangelog0.0.6Use_default_managerinstead ofobjectsmanager to allow for overrides.0.0.5Only pass fields that support an exact lookup to get_or_create.0.0.4Useexistsoptimisation.Force commit on every execution of method since settings may specify a transaction managed database layer.Prevent redundant calls to save().Support direct assignment of foreign key ids.0.0.3 (2011-08-12)Corrected manifest.0.0.2 (2011-07-26)Docs.0.0.1Initial release.