Posted on

django seeds

And here’s that same fixture as YAML:

Documentation

Providing initial data for models¶

It’s sometimes useful to pre-populate your database with hard-coded data when you’re first setting up an app. You can provide initial data with migrations or fixtures.

Providing initial data with migrations¶

If you want to automatically load initial data for an app, create a data migration . Migrations are run when setting up the test database, so the data will be available there, subject to some limitations .

Providing data with fixtures¶

A fixture is a collection of data that Django knows how to import into a database. The most straightforward way of creating a fixture if you’ve already got some data is to use the manage.py dumpdata command. Or, you can write fixtures by hand; fixtures can be written as JSON, XML or YAML (with PyYAML installed) documents. The serialization documentation has more details about each of these supported serialization formats .

As an example, though, here’s what a fixture for a simple Person model might look like in JSON:

And here’s that same fixture as YAML:

You’ll store this data in a fixtures directory inside your app.

Loading data is easy: just call manage.py loaddata , where is the name of the fixture file you’ve created. Each time you run loaddata , the data will be read from the fixture and re-loaded into the database. Note this means that if you change one of the rows created by a fixture and then run loaddata again, you’ll wipe out any changes you’ve made.

Where Django finds fixture files¶

By default, Django looks in the fixtures directory ins > FIXTURE_DIRS setting to a list of additional directories where Django should look.

When running manage.py loaddata , you can also specify a path to a fixture file, which overrides searching the usual directories.

Fixtures are also used by the testing framework to help set up a consistent test environment.

Django-seed allows you to write code to generate models, and seed your database with one simple manage.py command!

django-seed 0.1.9

pip install django-seed Copy PIP instructions

Last released: May 14, 2018

Seed your Django project with fake data

Navigation

Project links

Statistics

View statistics for this project via Libraries.io, or by using Google BigQuery

License: MIT License (MIT)

Tags faker, fixtures, data, test, django, seed

Maintainers

Classifiers

  • Development Status
    • 3 – Alpha
  • Framework
    • Django
    • Django :: 1.11
    • Django :: 2.0
  • Intended Audience
    • Information Technology
  • License
    • OSI Approved :: MIT License
  • Natural Language
    • English
  • Programming Language
    • Python
    • Python :: 2
    • Python :: 2.7
    • Python :: 3
    • Python :: 3.4
    • Python :: 3.5
    • Python :: 3.6
  • Topic
    • Software Development :: Libraries :: Python Modules
    • Software Development :: Testing
    • Utilities

Project description

Django-seed uses the faker library to generate test data for your Django models. This has been “hard-forked” from django_faker in order to support newer versions of Python and Django

Django-seed allows you to write code to generate models, and seed your database with one simple manage.py command!

Installation

To install django-seed, use pip:

Or to install from source:

Configuration

Add it to your installed apps in settings.py:

Usage

Note: When seeding models with Foreign Keys, you need to make sure that those models are seeded first. For example, if a model in app A has a foreign key to a model in app B , you must seed app B first.

Using with command

With django-seed, you can seed your database with test data from the command line using the manage.py seed command.

Ex: Seed 15 of each model for the app api:

That’s it! Now you have 15 of each model seeded into your database.

Using with code

django-seed provides methods to easily seed test databases for your Django models. To seed your database with Model instances, import Seed, get a seeder instance, and use the add_entity method.

Ex: seeding 5 Game and 10 Player objects:

The seeder uses the name and column type to populate the Model with relevant data. If django-seed misinterprets a column name, you can still specify a custom function to be used for populating a particular column, by adding a third argument to the add_entity() method:

Django-seed does not populate auto-incremented primary keys, instead seeder.execute() returns the list of inserted PKs, indexed by class:

Tests

To run django tests in a django environment, first make sure you have the packages from requirement-test.txt installed, then run the following:

or if you have django_seed in INSTALLED_APPS: