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 ?
According to django 3.1 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. e.g. Or, you can write fixtures by hand; fixtures can be written as JSON, XML or YAML.
You can load data by calling manage.py loaddata <fixturename> , where <fixturename> is the name of the fixture file you've created. Each time you run loaddata , the data will be read from the fixture and reloaded into the database.
Project description *django-populate* provides an adapter for Django Models, for easy population of test databases. To populate with Model instances, create a new Populator class, then list the class and number of all of Models that must be generated. To launch the actual data population, call `execute()` method.
django-admin is Django's command-line utility for administrative tasks. This document outlines all it can do. In addition, manage.py is automatically created in each Django project.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With