Skip to main content

A library to facilitate the creation of fake data (seeds) in Django projects.

Project description

jessilver_django_seed

jessilver_django_seed is a library for the Django framework that facilitates the creation of data (seeds) to populate the database during development and testing. With jessilver_django_seed, you can quickly generate realistic data for your Django applications, which is useful for testing functionalities and visualizing how the application behaves with different types of data.

Features

  • Fake data generation: Easily create test data for your Django models.
  • Simple configuration: Easy integration with existing Django projects.

Configuration

Installation

Install the library:

pip install jessilver_django_seed

Adding to the Project

Add jessilver_django_seed to INSTALLED_APPS in your settings.py file:

INSTALLED_APPS = [
    ...
    'jessilver_django_seed',
]

Create a constant called SEEDER_APPS to define the apps you want to populate with fake data:

SEEDER_APPS = [
    'app1',
    'app2',
    ...
]

Usage

Directory Structure

Inside the folder of the apps added in SEEDER_APPS, create a directory called seeders:

app1/
├── ...
├── seeders/
└── ...

Creating Seeders

Inside the seeders directory, you can create files to define the data you want to generate. For example, a file called user_seeder.py:

To create a seeder, you need to implement two main functions: seeder_name and seed. I will explain each of them:

  1. seeder_name

    This function is responsible for returning the name of the seeder. This name is generally used to uniquely identify the seeder within the system. It can be useful for organizational purposes and to ensure that the correct seeder is being executed.

    Example:

    def seeder_name():
        return "UserSeeder"
    
  2. seed

    This function is where the data insertion logic is implemented. It is responsible for populating the database with the desired data. The seed function usually contains commands to create records in the database, using models or direct queries.

    Example:

    def seed():
        users = [
            {"name": "Alice", "email": "alice@example.com"},
            {"name": "Bob", "email": "bob@example.com"},
        ]
        for user in users:
            # Assuming you have a User model
            User.create(**user)
    

Example of a complete seeder:

from jessilver_django_seed.seeders.BaseSeeder import BaseSeeder
from django.contrib.auth.models import User

class SuperUserSeeder(BaseSeeder):
    @property
    def seeder_name(self):
        return 'SuperUserSeeder'

    def seed(self):
        if not User.objects.filter(is_superuser=True).exists():
            User.objects.create_superuser(
                username='admin',
                email='admin@example.com',
                password='123456789',
                first_name='Admin',
                last_name='User'
            )
            self.success(f'Super User created')
        else:
            self.error(f'Super User already exists')

You can create multiple files or just one containing multiple classes. The only requirement is that the class names end with Seeder, otherwise, it will not work.

For example, you can create a file seeders.py with multiple classes:

from jessilver_django_seed.seeders.BaseSeeder import BaseSeeder
from django.contrib.auth.models import User
from myapp.models import Profile

class SuperUserSeeder(BaseSeeder):
    @property
    def seeder_name(self):
        return 'SuperUserSeeder'

    def seed(self):
        if not User.objects.filter(is_superuser=True).exists():
            User.objects.create_superuser(
                username='admin',
                email='admin@example.com',
                password='123456789',
                first_name='Admin',
                last_name='User'
            )
            self.success(f'Super User created')
        else:
            self.error(f'Super User already exists')

class ProfileSeeder(BaseSeeder):
    @property
    def seeder_name(self):
        return 'ProfileSeeder'

    def seed(self):
        for user in User.objects.all():
            Profile.objects.get_or_create(user=user, defaults={
                'bio': 'This is a bio',
                'location': 'Unknown'
            })
            self.success(f'Profile created for user {user.username}')

Running the Seeders

Now, you can run the command to populate the database with fake data:

python manage.py seed

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

jessilver_django_seed-1.0.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jessilver_django_seed-1.0.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file jessilver_django_seed-1.0.0.tar.gz.

File metadata

  • Download URL: jessilver_django_seed-1.0.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for jessilver_django_seed-1.0.0.tar.gz
Algorithm Hash digest
SHA256 058b177a76a401764342f85d5de4ff3e228525b8077b50d3b1a184a382feeba6
MD5 46deaa5393322f58531de088c7ef8a13
BLAKE2b-256 5323a168b2882e9f4dc8b4b988ee3416450488a2a50dd7bcdc9792dcd8cfc76a

See more details on using hashes here.

File details

Details for the file jessilver_django_seed-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for jessilver_django_seed-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 28882e54b800faadb8c5356d2f63e392d29ac041fa108299c8dc101d38691462
MD5 c3e780b99cd0115b4445a3be859e8720
BLAKE2b-256 5a5b592fab339bd254deefef9b4a62c577f406a311b65b19e3e5a00f09e155f5

See more details on using hashes here.

Supported by

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