Django for Intermediate Users: Django Models and Admin

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Installation and Setup
  4. Django Models
  5. Django Admin
  6. Recap

Introduction

Welcome to this Django tutorial for intermediate users. In this tutorial, we’ll be focusing on Django models and the Django admin interface. By the end of this tutorial, you will have a good understanding of how to define models in Django, create database tables based on those models, and use the Django admin interface to manage your data.

Prerequisites

To fully grasp the content of this tutorial, you should have a solid understanding of Python and the basics of Django. If you are unfamiliar with Django, I recommend going through the Django for Beginners tutorial first.

Installation and Setup

Before we begin, let’s make sure you have Django installed on your system. Open a terminal or command prompt and run the following command: shell pip install django Once Django is installed, we can proceed with creating our Django project and application. Open your terminal or command prompt and navigate to the desired directory where you want to create your project. Then, run the following command: shell django-admin startproject myproject This will create a new directory called “myproject” with the basic Django project structure.

Next, navigate into the “myproject” directory: shell cd myproject Now, let’s create a new Django application within our project: shell python manage.py startapp myapp This will create a new directory called “myapp” inside your project.

Django Models

Django models allow you to define the structure and behavior of your database tables. In this section, we’ll walk through the process of creating and using models in a Django application.

  1. Open the “myapp” directory and locate the “models.py” file.

    cd myapp
    
  2. Inside the “models.py” file, we’ll define our first model class. Let’s create a simple “Product” model with a few fields:

    from django.db import models
    
    class Product(models.Model):
        name = models.CharField(max_length=100)
        price = models.DecimalField(max_digits=6, decimal_places=2)
        description = models.TextField()
        created_at = models.DateTimeField(auto_now_add=True)
    

    In this example, we defined a “Product” model with four fields: “name”, “price”, “description”, and “created_at”. The “name” field is a character field with a maximum length of 100 characters. The “price” field is a decimal field with a maximum of 6 digits and 2 decimal places. The “description” field is a text field, and the “created_at” field will automatically set the current date and time when a new record is created.

  3. Now that we defined our model, let’s create the corresponding database table. Run the following command:

    python manage.py makemigrations
    

    This command generates a set of SQL statements that describe the changes to be made to your database schema based on your model definition.

  4. Apply the migrations by running:

    python manage.py migrate
    

    This command creates the actual database tables based on the migrations.

  5. We can now interact with our “Product” model using Django’s database API. Open the Django shell by running:

    python manage.py shell
    

    In the shell, you can create, retrieve, update, and delete records from the “Product” model. Here are a few examples:

    from myapp.models import Product
    
    # Create a new product
    product = Product(name="Example Product", price=9.99, description="A great product")
    product.save()
    
    # Retrieve all products
    products = Product.objects.all()
    
    # Update a product
    product = Product.objects.get(id=1)
    product.price = 14.99
    product.save()
    
    # Delete a product
    product = Product.objects.get(id=1)
    product.delete()
    

    These commands demonstrate the basic CRUD (Create, Retrieve, Update, Delete) operations you can perform on your models.

Django Admin

Django provides an admin interface that allows you to manage your application data without writing any additional code. In this section, we’ll explore how to set up and customize the Django admin interface.

  1. Open the “admin.py” file inside the “myapp” directory.

    cd myapp
    
  2. Add the following code to register the “Product” model in the admin interface:

    from django.contrib import admin
    from myapp.models import Product
    
    admin.site.register(Product)
    

    This code imports the necessary modules and registers the “Product” model with the Django admin site.

  3. Start the development server:

    python manage.py runserver
    

    Now, if you open your browser and navigate to “http://localhost:8000/admin/”, you should see the Django admin login page.

  4. To log in, you need to create a superuser by running the following command:

    python manage.py createsuperuser
    

    Follow the prompts to enter your desired username and password.

  5. Once you’re logged in, you can manage your “Product” records through the Django admin interface. You can add new products, edit existing ones, and delete them.

    Django Admin Interface

    Note: The screenshot is a placeholder. Replace the URL with a relevant image if needed.

Recap

In this tutorial, you learned how to define Django models, create database tables based on those models, and use the Django admin interface to manage your data. Here’s a brief recap of what we covered:

  • Installation and setup of Django
  • Defining Django models
  • Generating and applying migrations
  • Performing CRUD operations on models
  • Setting up and customizing the Django admin interface

Now that you understand the basics of Django models and the admin interface, you can continue exploring more advanced features and techniques to build powerful web applications with Django.