Models
Models
DJANGO MODELS
Defining a Model
- To define a model in Django, you create a class that inherits from
django.db.models.Model. Each attribute of the class represents a
database field.
- Example:
- First we have the import statement which imports the django models module.
- Secondly we start with the model definition along with the fields.
Django models Fields:
Django provides various field types that you can use to define your model's
attributes: Basically These Fields specify what type of data to be accepted from
the User.
- CharField: Used for short text fields. Requires a max_length
parameter to specify the maximum length.
-Ex: name = models.CharField()
- DateTimeField: Used for date and time. You can use auto_now_add
to set the field to the current date and time when the object is created,
and auto_now to update it each time the object is saved.
- Ex: created_at = models.DateTimeField(auto_now_add=True)
After creating or modifying a model in Django, the next important step is to create
and apply migrations. Migrations are Django's way of propagating changes which
you make to your models (like adding a field or deleting a model) into your
database schema
What are Migrations?
Migrations are files that Django generates to keep track of changes in your models and
apply those changes to your database schema. They allow you to update your database
structure without losing data.
1. Make Migrations
○ After creating or modifying a model, you need to generate a migration file.
This file records the changes you made to the model.
○ Command:
○ This command will scan your models and create a migration file in the
migrations directory of your app (Basically it converts python code to
sql query). The file will contain the instructions to apply the changes you
made to your models.
1. Apply Migrations
Migration Files
Migration files are automatically created in the migrations directory of your app.
They are named with a timestamp and a brief description of the change, like
0001_initial.py for the first migration or 0002_add_due_date.py if you added a
field, etc..
Migration Commands
The Django admin panel is a powerful built-in interface that allows you to manage your
application's data without having to write custom code for basic CRUD (Create, Read,
Update, Delete) operations.
How to set up and use the Django admin panel:
1. Creating a Superuser
To access the Django admin panel, you need to create a superuser. A superuser has all
permissions in the Django admin interface, including the ability to add, edit, delete, and
view all data.
Steps:
By default, the Django admin panel does not automatically display your models. You
need to explicitly register each model you want to manage through the admin interface.
● Registering a Model:
○ Open admin.py in your app's directory.
○ Import the model you want to register.
○ Use the admin.site.register() function to register your model.
Once your model is registered, you can perform CRUD operations (Create, Read,
Update, Delete) directly through the admin interface.
The Django shell allows you to interact with your Django project's models and database
directly from the command line. It's useful for testing queries, creating objects, and
debugging.
● Read:
○ all() method:
○ get() method:
○ filter() method:
● Update:
● Delete:
You can implement CRUD operations in Django views by defining functions that handle
different HTTP methods like GET and POST.
Django uses HTML forms to pass GET and POST requests. Here’s how you can create
simple forms for each operation.
After successfully handling a form submission, it's a good practice to redirect the user to
another page, such as a list view or a success page.
Redirects: The redirect() function is used to send an HTTP redirect to the user,
taking them to a different view or URL after an operation is completed.