Django Insert Data

What is Django Insert Data?

Django, a high-level Python web framework, offers an efficient way to insert data into a database using its built-in ORM (Object-Relational Mapping) system. The ORM allows developers to interact with the database using Python objects instead of SQL, simplifying the process of managing and inserting data.

Understanding Model Class in Django

In Django, a Model Class defines the structure of data stored in a database. It acts as a blueprint for creating database tables, specifying the fields and their types, such as text, integers, or dates. Django models also provide methods for creating, reading, updating, and deleting records in the database.

Defining a Database Table using Models

1. Open the models.py file of your app.

2. Import the necessary modules:

from django.db import models

3. Create a class that represents your table, inheriting from models.Model:

class MyModel(models.Model):    
		field_name = models.CharField(max_length=100)

4. Run python manage.py makemigrations to generate a migration file.

5. Apply the migration with python manage.py migrate.

Field Types and Model Definitions

Django offers various field types to define the structure of your data.

  • CharField: Stores strings with a maximum length.
  • DecimalField: Stores decimal numbers, with a specified number of digits and decimal places.
  • IntegerField: Stores whole numbers.

These fields help define the data structure and ensure consistency.

Creating a Patient Model

To create a Patient Model in Django:

1. Import the necessary modules:

from django.db import models

2. Create a class for the patient model:

class Patient(models.Model):    
		name = models.CharField(max_length=100)
    age = models.IntegerField()
    gender = models.CharField(max_length=10)
    phone = models.CharField(max_length=15)
    email = models.EmailField()

3. Run the migration commands to update the database schema.

Example of a Patient Model Class

class Patient(models.Model):
		name = models.CharField(max_length=100)
    age = models.IntegerField()
    gender = models.CharField(max_length=10)
    phone = models.CharField(max_length=15)
    email = models.EmailField()

This model represents a patient with fields for name, age, gender, phone, and email.

Employee Model and Object of Model

To create an Employee model:

1. Define the class structure:

class Employee(models.Model):
		name = models.CharField(max_length=100)
    age = models.IntegerField()
    salary = models.DecimalField(max_digits=8, decimal_places=2)

2. Create an instance of the model:

employee = Employee(name="John Doe", age=30, salary=5000.00)
employee.save()

Using Objects of a Model in Views

To use model objects in views:

1. Import the model in your view file:

from .models import Employee

2. Retrieve or manipulate the object in the view function:

employee = Employee.objects.get(id=1)

This enables dynamic interaction with the database in your application.

Article Model and File into Models

To create an Article model that incorporates files:

1. Define the model:

class Article(models.Model):    
		title = models.CharField(max_length=200)
    content = models.TextField()
    image = models.ImageField(upload_to='images/')

2. Migrate the model and use it to store and retrieve article data, including file uploads.

This structure allows easy management of articles and associated files in Django.

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate