Hi there, we’re Harisystems

"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".

For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.

1
1

Django Admin: Setting Fields to Display in the Administration Interface

Introduction

Django Admin provides flexibility in customizing the fields displayed in the administration interface for a model. By specifying the fields to display, you can focus on the most relevant information, improve usability, and enhance the user experience. In this guide, we will explore how to set fields to display in Django Admin with examples.

Step 1: Enabling Django Admin

Before we dive into setting fields to display, make sure you have Django Admin enabled in your project. If you haven't enabled Django Admin yet, you can refer to the previous guide on enabling Django Admin.

Step 2: Creating a Model

To demonstrate setting fields to display, let's assume we have a model called Product with multiple fields. Here's an example:


from django.db import models

class Product(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=8, decimal_places=2)
    description = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    # Additional fields and methods for the Product model
    

In this example, we define a Product model with fields such as name, price, description, created_at, and updated_at. You can have additional fields and methods based on your application's requirements.

Step 3: Customizing the Admin Interface

To customize the fields displayed in the Django Admin interface for the Product model, you need to create an admin class for the model. Open the admin.py file in your app directory and define an admin class for the Product model. Here's an example:


from django.contrib import admin
from .models import Product

class ProductAdmin(admin.ModelAdmin):
    list_display = ('name', 'price', 'created_at')

admin.site.register(Product, ProductAdmin)
    

In this example, we import the admin module from django.contrib and the Product model from the current app's models.py file. We then define a ProductAdmin class that inherits from admin.ModelAdmin. Inside the ProductAdmin class, we specify the list_display attribute to set the fields to display in the list view of the administration interface. In this case, we choose to display the name, price, and created_at fields. Finally, we register the Product model with the ProductAdmin class.

Step 4: Accessing the Admin Interface

To see the customized fields in the Django Admin interface, visit the /admin URL of your project (e.g., http://localhost:8000/admin). Log in with your superuser credentials to access the Django Admin dashboard.

Step 5: Viewing the Customized Fields

In the Django Admin dashboard, navigate to the Product model view. Here, you will see that the list view displays the specified fields: name, price, and created_at. Only these fields will be shown in the list view, simplifying the view and making it more focused.

Conclusion

Setting fields to display in the Django Admin interface allows you to customize the view and focus on the most relevant information for a model. By following this guide, you have learned how to enable Django Admin, create an admin class, set the fields to display using the list_display attribute, and view the customized fields in the Django Admin interface. With Django Admin, you can enhance the user experience and improve the usability of your Django web applications.

4.5L

Learners

20+

Instructors

50+

Courses

6.0L

Course enrollments

4.5/5.0 5(Based on 4265 ratings)

Future Trending Courses

When selecting, a course, Here are a few areas that are expected to be in demand in the future:.

Beginner

The Python Course: Absolute Beginners for strong Fundamentals

By: Sekhar Metla
4.5 (13,245)
Intermediate

JavaScript Masterclass for Beginner to Expert: Bootcamp

By: Sekhar Metla
4.5 (9,300)
Intermediate

Python Coding Intermediate: OOPs, Classes, and Methods

By: Sekhar Metla
(11,145)
Intermediate

Microsoft: SQL Server Bootcamp 2023: Go from Zero to Hero

By: Sekhar Metla
4.5 (7,700)
Excel course

Future Learning for all

If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.

View Courses

Most Popular Course topics

These are the most popular course topics among Software Courses for learners