How to seed Django project ? - insert a bunch of data into the project for initialization

半腔热情 提交于 2020-12-25 00:32:21

问题


I'ved been developing in Django and was wondering if there is a way to seed data into the database in Django.

In ruby on rails, I use seed.rb and then run "rake db:seed" in command line.

Main reason I want to seed some data on statuses, types, etc for the project initialization.

Is there something similar ?


回答1:


Similar to Rails, we also have option to seed the database. It is done using management commands. In one of your apps, use the following folder structure

<project>/<app>/management/commands/seed.py

this makes python manage.py seed available as a management command. I personally follow the following structure.

# <project>/<app>/management/commands/seed.py
from django.core.management.base import BaseCommand
import random

# python manage.py seed --mode=refresh

""" Clear all data and creates addresses """
MODE_REFRESH = 'refresh'

""" Clear all data and do not create any object """
MODE_CLEAR = 'clear'

class Command(BaseCommand):
    help = "seed database for testing and development."

    def add_arguments(self, parser):
        parser.add_argument('--mode', type=str, help="Mode")

    def handle(self, *args, **options):
        self.stdout.write('seeding data...')
        run_seed(self, options['mode'])
        self.stdout.write('done.')


def clear_data():
    """Deletes all the table data"""
    logger.info("Delete Address instances")
    Address.objects.all().delete()


def create_address():
    """Creates an address object combining different elements from the list"""
    logger.info("Creating address")
    street_flats = ["#221 B", "#101 A", "#550I", "#420G", "#A13"]
    street_localities = ["Bakers Street", "Rajori Gardens", "Park Street", "MG Road", "Indiranagar"]
    pincodes = ["101234", "101232", "101231", "101236", "101239"]

    address = Address(
        street_flat=random.choice(street_flats),
        street_locality=random.choice(street_localities),
        pincode=random.choice(pincodes),
    )
    address.save()
    logger.info("{} address created.".format(address))
    return address

def run_seed(self, mode):
    """ Seed database based on mode

    :param mode: refresh / clear 
    :return:
    """
    # Clear data from tables
    clear_data()
    if mode == MODE_CLEAR:
        return

    # Creating 15 addresses
    for i in range(15):
        create_address()

In above structure you could, add custom modes, and seed accordingly. Also you could add additional management command arguments (e.g. number_of_addresses and pass it to run seed. the command would be python manage.py seed --mode=refresh --number_of_addresses=15).

Hope this helps. Cheers!




回答2:


According to django 3.0 docs You can seed your database by creating fixtures. 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.

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

[
  {
    "model": "myapp.person",
    "pk": 1,
    "fields": {
      "first_name": "John",
      "last_name": "Snow"
    }
  },
  {
    "model": "myapp.person",
    "pk": 2,
    "fields": {
      "first_name": "Paul",
      "last_name": "McCartney"
    }
  }
]

And here’s that same fixture as YAML:

- model: myapp.person
  pk: 1
  fields:
    first_name: John
    last_name: Snow
- model: myapp.person
  pk: 2
  fields:
    first_name: Paul
    last_name: McCartney

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

Loading data is easy: just call manage.py loaddata <fixturename>, where <fixturename> is the name of the fixture file you’ve created.




回答3:


You can create a data migration.... https://docs.djangoproject.com/en/2.0/topics/migrations/#data-migrations

python manage.py makemigrations --empty appname



回答4:


I am use this lib

https://pypi.org/project/django-seed/

from django_seed import Seed

seeder = Seed.seeder()

from myapp.models import Game, Player
seeder.add_entity(Game, 5)
seeder.add_entity(Player, 10)

inserted_pks = seeder.execute()



回答5:


There are multiple ways to provide seed data to Django:

If you are prototyping and don't care yet about maintaining consistency across environments, using Django's dumpdata and loaddata is very easy and fast:

$ ./manage.py dumpdata myApp.MyModel [myApp.AnotherModel] --output <myfile>
$ ./manage.py loaddata <path-to-myfile>

The fixture files produced can be different formats (json by default) so they are easy to edit or generate and they include information about which app/model they reference, so they are easy to load.

If you need to maintain consistency across environments, either:

Use data migrations:

./manage.py makemigrations --empty <myapp> --name seed_<my_model>

Or add a custom management command per @anurag's answer



来源:https://stackoverflow.com/questions/51577441/how-to-seed-django-project-insert-a-bunch-of-data-into-the-project-for-initi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!