Django, a powerful Python web framework, simplifies the process of building robust web applications, including database management. Central to Django’s data handling is the concept of models, which define the structure of your application’s database tables. In this blog post, we’ll provide a comprehensive guide on how to create and register models in Jango.
Before we begin, make sure you have the following prerequisites:
- Django Installed: Ensure that you have Django installed in your Python environment. You can install it using pip:
pip install Django
- Django Project: Set up a Django project if you haven’t already, including creating a new Django app or using an existing one.
- Basic Understanding of Django: Familiarity with Django’s project structure, views, templates, and database concepts is beneficial.
Creating and Registering Models
Follow these steps to create and register models in Django:
Step 1: Define Your Model
1.1. Open your Django app’s
models.py file. This is where you define your models.
1.2. Define a model class by creating a Python class that inherits from
django.db.models.Model. For example, let’s create a simple
# models.py from django.db import models class Product(models.Model): name = models.CharField(max_length=100) description = models.TextField() price = models.DecimalField(max_digits=10, decimal_places=2) is_published = models.BooleanField(default=False) def __str__(self): return self.name
In this example, we’ve created a
Product model with fields for name, description price and is_published.
Step 2: Migrate Your Model
Django uses migrations to create and update database tables based on your model definitions.
2.1. Create an initial migration for your app:
python manage.py makemigrations your_app_name
your_app_name with the name of your Django app.
2.2. Apply the migrations to create the database table:
python manage.py migrate
Step 3: Register Your Model with the Admin Panel
Django’s admin panel provides a convenient interface for managing your database records. To make your model accessible via the admin panel:
3.1. Create an admin file for your app if it doesn’t already exist. In your app’s directory, create a file named
3.2. Register your model in the
# admin.py from django.contrib import admin from .models import Product admin.site.register(Product)
This code imports the
Product model and registers it with the admin panel.
Step 4: Customize the Admin Panel (Optional)
You can further customize how your model is displayed in the admin panel by creating an admin class for it. For instance, you can specify which fields to display, search by, and filter on in the admin list view.
# admin.py from django.contrib import admin from .models import Product class ProductAdmin(admin.ModelAdmin): list_display = ('name', 'price') list_filter = ('price',) search_fields = ('name',) admin.site.register(Product, ProductAdmin)
Learn more about How to Customize Django Admin Interface
Step 5: Create Superuser
Before accessing the admin panel, create a superuser account with administrative privileges:
python manage.py createsuperuser
Follow the prompts to set up your superuser account.
Step 6: Run the Development Server
Start your Django development server:
python manage.py runserver
Access the admin panel in your web browser at
http://127.0.0.1:8000/admin/, and log in using the superuser credenttials.
Step 7: Add Data via the Admin Panel
In the admin panel, you can now add, view, edit, and delete
Product records. Click “Products” under your app’s section to manage the data.
Creating and registering models in Django is a fundamental step in building database-driven web applications. With the right models defined and registered in the admin panel, you can easily manage your application’s data. In this blog post, we’ve covered the assential steps to define, migrate, register, and customize models in Django. This knowledge will empower you to design and implement your database schema effectively as you develop your Django applications.
Find this tutorial on Github.