Model & Django Admin Area
In Django, a model is a Python class that represents a database table. It defines the fields and behavior of the data that will be stored in the corresponding database table. Models in Django follow the Object-Relational Mapping (ORM) paradigm, which means that you can interact with the database using high-level Python objects instead of directly writing SQL queries.
Here are the key aspects of the Django admin area:
1. Define Models:
In your app directory (todo/models.py
), define the data models for todo app. This is where you specify the structure of your database tables.
Here is a TodoList Django model:
Your Task
model in Django represents a table in the database with the following fields:
user
: A foreign key relationship to theUser
model fromdjango.contrib.auth.models
. It establishes a many-to-one relationship between tasks and users, meaning each task is associated with a single user, and each user can have multiple tasks. Theon_delete=models.CASCADE
argument means that if a user is deleted, all tasks associated with that user will also be deleted.title
: A character field for the title of the task.description
: A text field for a more detailed description of the task. It allows null values, which means a task can exist without a description.complete
: A boolean field indicating whether the task is complete or not. It has a default value ofFalse
.created
: A DateTimeField that automatically sets the current date and time when a task is created.__str__
: A method that defines a human-readable string representation of each instance of theTask
model. In this case, it returns the title of the task.Meta
: A class within the model that can be used to configure various options. In this case,order_with_respect_to
is set to'user'
, indicating that the tasks should be ordered with respect to the associated user.
This model can be used to store information about tasks, including who the task belongs to, its title, description, completion status, and creation timestamp. The User
foreign key establishes a connection between tasks and users, allowing you to associate tasks with specific users in your application.
2. Model Registration:
By default, all models are available in the admin interface, but you can customize how they are displayed by creating an admin.py
file in your app and registering your models there.
3. Migrate the model (Task):
Django uses migrations to manage database schema changes. Run the following commands to apply your models to the database: (refer fig. 2.3 & fig. 2.4)
In summary, this output represents the successful application of database migrations for the specified Django applications, ensuring that the database schema is up to date with the latest changes defined in the migrations.
4. Django Admin Area Installation:
Django's admin area is a powerful and customizable feature that comes built-in with the Django web framework. It provides a user-friendly interface for managing and interacting with your application's data. The admin area is automatically generated based on your models, allowing you to perform CRUD (Create, Read, Update, Delete) operations on your database records without having to write custom views and templates.
To use the admin interface, you need to make sure it's enabled in your Django project. In your urls.py
file, you should have something like:
This sets up the admin URLs, and you can access the admin interface at http://yourdomain.com/admin/
.
5. Creating Admin Users:
Admin users are different from regular users. You need to create an admin user to access the admin interface. You can do this using the createsuperuser
management command: (refer fig. 2.5)
6. Django Admin:
This sets up the admin URLs, and you can access the admin interface at http://127.0.0.1:8000/admin/
. Type the below request in web browser (refer fig. 2.6 and fig. 2.7)
7. Adding some tasks to the defined model
Last updated