Django's admin interface offers a quick way to view, add, edit, and delete database entries through a web page. This saves time and effort compared to building custom management tools.
Registering a model with the admin site makes it visible and manageable through the admin UI, allowing CRUD operations on its data.
from django.contrib import admin from .models import Product class ProductAdmin(admin.ModelAdmin): list_display = ('name', 'price', 'stock') admin.site.register(Product, ProductAdmin)
Setting list_display in ModelAdmin customizes the columns shown on the model's list page in the admin interface.
from django.contrib import admin from .models import Customer admin.site.register(Customer, admin.ModelAdmin)
There is no syntax error; the code works fine. Passing admin.ModelAdmin directly uses Django's default ModelAdmin class for the model, which is equivalent to omitting the second argument.
from django.contrib import admin from .models import Order class OrderAdmin(admin.ModelAdmin): list_display = ('id', 'customer_name', 'total') admin.site.register(Order, OrderAdmin)
If a field listed in list_display does not exist on the model or as a method/property on the ModelAdmin, Django raises an AttributeError when rendering the admin list page.