Skip to main content

Automatically generate reasonable database entries for your app

Project description

Introduction

Django-Scaffolding creates pseudo-real-world placeholder data for your app. Data can be any type like names, cities, images and instances of your models. It’s not a mocking framework, it creates real django model instances.

Usage

Create a scaffolds.py module within your app directory which contains the Scaffolding classes.

Sample models.py:

class Entry(models.Model):
    first_name = models.CharField('First Name', max_length=32)
    last_name = models.CharField('Last Name', max_length=32)
    comment = models.TextField('Comment')
    image = models.ImageField(upload_to='uploads/%Y/%m/%d', blank=True, null=True)
    contest = models.ForeignKey(Contest)
    ...

Sample scaffolds.py:

import scaffolding
from scaffolding.library.flickr import FlickrInteresting

class EntryScaffold(object):
    first_name = scaffolding.FirstName(max_length=32)
    last_name = scaffolding.LastName(max_length=32)
    comment = scaffolding.LoremIpsum(paragraphs=1)
    contest = scaffolding.ForeignKey(queryset=Contest.objects.filter(name='testcontest'))
    image = scaffolding.RandomInternetImage(backend=FlickrInteresting)

scaffolding.register(Entry, EntryScaffold)

Mind the syntax for ForeignKey fields. You can assign an integer to the field but make sure the element with the corresponding key does exist. Of course you can also assign an object to the FK field.

To use the flickr library you need to have the Flickr API: http://stuvel.eu/flickrapi installed.

Run the management command to create the data:

manage.py scaffold myapp.MyModel 20

The number stands for the number of entries to be created.

Using scaffolding in the interpreter or in views

You can try out the included classes or your own modules in the shell. All classes are generators (called Tubes) that generate the values for the fields. start ./manage.py shell:

>>> from scaffolding import *
>>> r = RandInt(min=1, max=5)
>>> r.next()
[4]
>>> r.next()
[2]

>>> n = Name(gender='m')
>>> n.next()
[u'Ethan Schmid']
>>> n.next()
[u'Michael Schneider']

Included Tubes

Name

Generates a random name. <gender> can be ‘male’, ‘female’, ‘m’ or ‘f’.

FirstName and LastName

Generates only first or last name. Takes the gender attribute as well.

LoremIpsum

Generates a Lorem Ipsum Text. The number of paragraphs is defined in paragraphs.

RandInt

Generates a random integer between min and max.

ForeignKey

Takes a queryset and iterates through it. Assigns the item as ForeignKey to the field. Wraps around if there are not enough items.

ForeignKeyOrNone

The same for nullable ForeignKeys. split is the weight for positives. 0.2 yields 80% None.

RandomInternetImage

Creates a random image for an ImageField using an internet source. A Flickr ‘Daily Interesting images’ grabber is included.

RandomDate

Creates a random date between startdate and enddate. startdate and enddate have to be datetime.date instances.

UsCity

Returns a name of a US city and state. e.g. “New York, NY”.

BookTitle

Creates a book title. This is a python implementation of the Random Title Generator.

URL

Creates a linkable to URL from a list of about 10000 URLs.

RandomEmail

Creates a random email. Parameters are length and domain.

AlwaysTrue

Returns True

AlwaysFalse

Returns False

TrueOrFalse

Randomly returns true or false. You can set a ratio for true or false by specifying true or false: e.g. false=3 returns 3 times as many False than Trues.

StaticValue

Takes one argument value and assigns it to the field.

RandomValue

Takes a list (not an iterable) as its lst argument and returns an element from it. You can use this for choice fields as well:

[c[0] for c in MyModel.MYCHOICES]

Every Value

Takes an iterable as its values argument and loops through them in order.

OrNone

This is a special tube that takes another tube as its first argument. It assigns a value from the passed class or None. This is useful for nullable fields. You can pass the arguments for the wrapped class as arguments to the OrNone class. There is one additional argument: split. This defines a ratio of useful to None. A ratio of 0.2 will give you 80% None.

OrBlank

The same as OrNone, but uses a blank string instead of None. Ideal for text fields that have blank=True.

Contrib

Crates a Custom Object. The backend class is the first parameter. The backend class has to inherit from Tube:

user = scaffolding.Contrib(FacebookTestUser, app_name='contest')

FacebookTestUser

Creates a Facebook User from the test users pool of the Facebook app. If there aren’t enough test users new ones are automatically created. This requires the django-facebook-graph API. https://github.com/feinheit/django-facebook-graph

The module is in external.facebook_graph.

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-scaffolding-0.2.3.tar.gz (13.5 kB view details)

Uploaded Source

File details

Details for the file django-scaffolding-0.2.3.tar.gz.

File metadata

File hashes

Hashes for django-scaffolding-0.2.3.tar.gz
Algorithm Hash digest
SHA256 a8c68ea36192946fe12f8e5785f787a06bb8624e1ab64821ef41d2f116067e81
MD5 5f46dfb37d9a65c1f4bcdddddd5d0545
BLAKE2b-256 394af7c4e1fa1ebdb8dd4016775fe1d53729855292cba5b44837a0aef6883ea0

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