Skip to main content

Helper class for generate sampledata

Project description

Django Sample Data Helper

http://kaleidos.net/static/img/badge.png https://travis-ci.org/kaleidos/django-sampledatahelper.png?branch=master https://coveralls.io/repos/kaleidos/django-sampledatahelper/badge.png?branch=master https://pypip.in/v/django-sampledatahelper/badge.png https://pypip.in/d/django-sampledatahelper/badge.png

App to automatically populate django database.

Install and configure

Install using pip, including any pillow if you want image genetion…:

pip install django-sampledatahelper
pip install pillow  # For image generation

You can configure, if you want a SAMPLEDATAHELPER_SEED variable in your settings, to generate alwais the same data. Example:

SAMPLEDATAHELPER_SEED = 123456789

If you want to use the sampledatafiller command, you have to define your SAMPLEDATAHELPER_MODELS with the list of models you want to fill. Example:

SAMPLEDATAHELPER_MODELS = [
    # Generate 5 instances completly random
    { 'model': 'myapp.MyModel', 'number': 5, },

    # Generate 5 instances selecting random method for some fields
    {
        'model': 'myapp.MyModel',
        'number': 5,
        'fields_overwrite': [
            ('my_int_field', lambda _, sd: sd.int(5, 10)),
        ]
    },

    # Generate 5 instances with fixed data in a field
    {
        'model': 'myapp.MyModel',
        'number': 5,
        'fields_overwrite': [
            ('my_int_field', 5),
        ]
    }
]

Quick start

Follow the install and configure instructions.

With Django sampledatahelper you have 2 options to populate your database

Using SampleDataFiller

Sample data filler is a command that use the SAMPLEDATAHELPER_MODELS setting variable to populate your database. Example:

SAMPLEDATAHELPER_MODELS = [
    # Generate 5 instances completly random
    { 'model': 'myapp.MyModel', 'number': 5, },

    # Generate 5 instances selecting random method for some fields
    {
        'model': 'myapp.MyModel',
        'number': 5,
        'fields_overwrite': [
            ('my_int_field', lambda _, sd: sd.int(5, 10)),
        ]
    },

    # Generate 5 instances with fixed data in a field
    {
        'model': 'myapp.MyModel',
        'number': 5,
        'fields_overwrite': [
            ('my_int_field', 5),
        ]
    }
]

Then you only have to run:

python manage.py sampledatafiller

Using a custom sampledata command

You can create a command to fill your models manullay to take more control.

If you have some aplications to populate, you can split your sample data generation on one command per app, or add only one command in one app thats generate everything.

The file must be in <app-module>/management/commands/<command-name>.py can be something like myapp/management/commands/mysampledata.py.

The easy way to build your command is using ModelDataHelper.

from django.core.management.base import BaseCommand
from myapp.models import MyModel
from sampledatahelper.model_helper import ModelDataHelper
from sampledatahelper.helper import SampleDataHelper

class Command(BaseCommand):
    args = ''
    help = 'Example data generator'
    mdh = ModelDataHelper(seed=12345678901)

    def handle(self, *args, **options):
        print "Generating MyModel data"
        # Generate 5 instances completly random
        self.mdh.fill_model(MyModel, 5)

        # Generate 5 instances selecting random method for some fields
        self.mdh.fill_model(MyModel,
                            5,
                            my_int_field=lambda instance, sd: sd.int(5, 10))

        # Generate 5 instances with fixed data in a field
        self.mdh.fill_model(MyModel, 5, my_int_field=8)

You can build a more precise command using directly the SampleDataHelper.

from django.core.management.base import BaseCommand
from myapp.models import MyModel
from sampledatahelper.helper import SampleDataHelper

class Command(BaseCommand):
    args = ''
    help = 'Example data generator'
    sd = SampleDataHelper(seed=12345678901)

    def generate_mymodel_data(self, instances):
        for x in range(instances):
            instance = MyModel.objects.create(
                slug=self.sd.slug(2, 3),
                name=self.sd.name(2, 3),
                claim=self.sd.sentence(),
                description=self.sd.paragraph(),
                email=self.sd.email(),
                photo=self.sd.image(64, 64),
                is_active=self.sd.boolean(),
                birth_date=self.sd.past_date(),
                expected_death_date=self.sd.future_date(),
                my_related_object=self.sd.db_object(MyRelatedModel)
            )

    def handle(self, *args, **options):
        print "Generating MyModel data"
        self.generate_mymodel_data(5)

To generate your sampledata, simply run the created command, for example:

python manage.py mysampledata

Documentation

Read the Docs: https://django-sample-data-helper.readthedocs.org/en/latest/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-sampledatahelper-0.2.1.tar.gz (178.7 kB view details)

Uploaded Source

File details

Details for the file django-sampledatahelper-0.2.1.tar.gz.

File metadata

File hashes

Hashes for django-sampledatahelper-0.2.1.tar.gz
Algorithm Hash digest
SHA256 1fa43516e4bc5fb153998c6f2967c99ee715ad43277c94ceddcc2ffaaa790453
MD5 3c82d8d9f4d011116ab81b8195a1cf2c
BLAKE2b-256 f1a4120099717043957a485e578ab3c601c223b13f85c2732ffaea9c8cc04d5c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page