Sunday 16 September 2018

Load initial data into Django model

This article explains pre-populating or seeding the core tables in django with initial data. It may be helpful to insert tables like status,employee types automatically while port source code from one place to another. Example situations like copy code from development server to production server. You can provide this  initial data with migrations or fixtures.

Here I am explaining about load data using fixtures. A fixture is a collection of data that Django knows how to import into a database. Fixtures can be written as JSON, XML or YAML formats. This tutorial is based on JSON fixture. This tutorial is writing in django 2 but it works fine with django 1.11 also. I am using Ubuntu 16.

These are the steps ,first create a file named fixtures.json then write the data in json format, then run the command to write this data into database.

First, we need a django project. For that, create a directory named my_proj somewhere in your home directory. Open that folder, then open a terminal window in that location. Then run these commands to install Django.

virtualenv -p python3 env
source env/bin/activate
pip install Django


Then create a project named my_proj and app named my_app by run following commands

django-admin startproject my_proj .
django-admin startapp my_app


For detailed tutorial about installing Django locally, then create a project go to this link:- http://newprograminglogics.blogspot.com/2018/02/install-python-3-and-set-up-local.html

Open my_proj/my_proj/settings.py then add  'my_app' to the INSTALLED_APPS section. Now it will look like this

INSTALLED_APPS = [
         'django.contrib.admin',
         'django.contrib.auth',
         'django.contrib.contenttypes',
         'django.contrib.sessions',
         'django.contrib.messages',
         'django.contrib.staticfiles',
         'my_app'
]


The project is configured now. We need to create two sample models named Status and Employee_type.
For this add the following code in my_proj/my_proj/my_app/models.py

class Status(models.Model):
    name = models.CharField(max_length=200)
    meta_name = models.CharField(max_length=200)

    class Meta:
    db_table = 'status'



class Employee_type(models.Model):
    name = models.CharField(max_length=200)
    meta_name = models.CharField(max_length=200)

    class Meta:
    db_table = 'employee_type'


Run these commands to create the actual tables in the database.
python manage.py makemigrations
python manage.py migrate


Create a directory named fixtures in my_proj/my_proj/my_app directory. Then create a file named fixtures.json inside this directory.
Open this file and paste the following code in it


[
  {
    "model": "my_app.Status",
    "pk": 1,
    "fields": {
      "name": "Pending",
      "meta_name": "pending"
    }
  },
  {
    "model": "my_app.Status",
    "pk": 2,
    "fields": {
      "name": "Approved",
      "meta_name": "approved"
    }
  },
  {
    "model": "my_app.Status",
    "pk": 3,
    "fields": {
      "name": "Rejected",
      "meta_name": "rejected"
    }
  },


  {
    "model": "my_app.Employee_type",
    "pk": 1,
    "fields": {
      "name": "Admin",
      "meta_name": "admin"
    }
  },
  {
    "model": "my_app.Employee_type",
    "pk": 2,
    "fields": {
      "name": "Staff",
      "meta_name": "staff"
    }
  }
]



Look into the json data each element contains three fields named model,pk,fields. As the name says they are the model name, id of table row and fields of the table row respectively. In the model field the my_app represents the app name Status,Employee_type are the models.

Our fixture is ready now. Run this command to insert this fixture to the tables we created earlier
python manage.py loaddata fixtures.json


Here we are using sqlite db so open db.sqlite3 file in my_proj directory using a sqlite browser software.
Check the status and employee_type tables. The values provided in the fixtures.json is inserted into the tables.

You can download the example source code from here:-
https://dl.dropboxusercontent.com/s/dhrqxgkjuzlwze6/my_proj.zip

Watch the video tutorial here (Click the gear icon and select video quality to 1080p HD, then click the full screen button for good quality)

Go to this link to subscribe to my YouTube channel to stay updated:- https://www.youtube.com/channel/UCxTp2yDew9m48Ch_hArOoYw 

1 comment:

  1. Articles can the impact of social media on body image and self-esteem. Deal Of NordVPN Articles can delve into the psychology of decision-making in marketing and advertising.

    ReplyDelete

Search This Blog