Generating Dummy Data in Django

Learn via video courses
Topics Covered

Overview

Model in Django is the way the data is stored. Django uses ORM(Object Relational Mapping) to link code to data. The Django web framework includes a default object-relational mapping layer (ORM), that can be used to interact with data from various relational databases such as SQLite, PostgreSQL, and MySQL. Django allows us to add, delete, modify, and query objects, using this API called ORM.

Introduction

A Database is a collection of data in an organized manner. It is also known as structured data. Django supports some of the standard Database programs like PostgreSQL, SQLite, MySQL, etc. The Django database consists of models we make for our project. Every model in Django corresponds to a single database table. The data we enter in the Django model becomes a record for the database table. When coding, it is sometimes necessary to test functionality using sample data, and manually constructing a sample dataset is time-consuming and tiresome. Dummy data Django can be used for testing as well as operational purposes. Dummy data is generated at random and used to replace live data in testing environments. The dummy data is needed in the following:

  1. When the number of possible states for our data is limited.
  2. When it's simple to recreate states with a few manual steps.

Packages to Create Dummy Data

Before working with actual data, it is crucial to verify and assess software and hardware with dummy data Django. This enables us to detect bugs. A Python library faker can easily create fake data. Django faker makes use of Python faker to create test data for Django models and templates. Django-faker enables an easy population of test data for Django models. To install Django faker we write the following command, after activating the virtual environment:

After the installation, we need to go to the settings.py file, We include django_faker under the INSTALLED_APP section.

In the above code,

  • In the FAKER_LOCALE we load settings.LANGUAGE_CODE.
  • In the FAKER_PROVIDERS we load faker.DEFAULT_PROVIDERS.

Using fake generators, we can generate fake data.

Another package is Django Bakery. A set of tools for exporting your Django site as flat files. Model Bakery provides a clever way to create fixtures for Django testing. Many objects can be created with a single line of code using a simple and powerful API. Well, to install Django bakery, we write the following code,

Suppose we have an app store. In the models.py,

In the test.py,

In the above code,

  • A class CustTestModel is created under TestCase to test the model
  • The command baker.make('store.Cust') imports the model.

Generating Fake Data

Let us first create a project Music and an app fav, after activating the virtual environment.

Our very next step is to mention the app we created in the settings.py file, under INSTALLED_APPS,

Let us now create model singers in the models.py,

After this, we are supposed to make migrations and then migrate the changes by using the following command,

Now our next step is to install a faker and model bakery.

Now we create a script fake_data.py in the same directory as manage.py,

In the above code,

  • We imported factory and faker from Faker.
  • We imported our models.
  • We imported a Recipe from model_bakery, a recipe is a set of rules for generating data for your models.
  • We are using a for loop to generate 50 random values of the model.
  • We are using the recipe for our model Singer to generate fake data for name, address, and created_prof.
  • Since we used future in the command future_datetime(end_date="+30d", tzinfo=None) we will be given data in the form of dates from the future. Now we are supposed to run the script we made using the following command,

After this step, we will find generated data in our database. Our next take is to register our models in the admin.py file.

Viewing Our Dummy Data Using Django Admin Page

Django provides a default admin interface that is required to perform operations like create, read, update, and delete on the model directly. It reads a set of data that gives information about data from the model, to provide an instant interface where the user can handle the contents of the application. The admin app is activated by default and already added into the INSTALLED_APPS present in the settings.py file. localhost:8000/admin/ is used to access the admin interface in the browser. It then asks the user to log in, if no log-in ID is created the following command will create a new superuser:

After creating the superuser, we start the server using the command:

Open the URL localhost:8000/admin/ in the browser again, enter your username and password then login. A Django Admin Dashboard opens where we can view our dummy data Django.

Conclusion

Hello Developer! I am sure by now you must have got an idea about dummy data Django and its generation. Let us summarize what we have learned through

  • Django uses ORM(Object Relational Mapping) to link code to data.
  • A Database is a collection of data in an organized manner.
  • The Django database consists of models we make for our project. Every model in Django corresponds to a single database table.
  • Dummy data is generated at random and used to replace live data in testing environments.
  • Django faker makes use of Python faker to create test data for Django models and templates.
  • Django-faker enables an easy population of test data for Django models.
  • Django model bakery is a set of tools for exporting your Django site as flat files. Model Bakery provides a clever way to create fixtures for Django testing.
  • A recipe is a set of rules for generating data for your models.
  • Django provides a default admin interface that is required to perform operations like create, read, update, and delete on the model directly.