Code-Memo

Admin Interface

Django provides you with an admin interface to perform CRUD (Create, Read, Update, Delete) operations on your models. To access the admin interface, you need to create a superuser by running:

python manage.py createsuperuser

Follow the prompts to create a superuser account. Once created, you can log in to the admin interface by navigating to http://127.0.0.1:8000/admin.

Registering Models

When you create a model (which we will discuss in detail later), it won’t automatically appear in the admin panel. You need to register it first. To do this, go to admin.py in your app directory and add the following:

from django.contrib import admin
from .models import MyModel

admin.site.register(MyModel)

This registers MyModel with the admin site, allowing you to manage it through the admin interface.

Customizing the Admin Interface

The Django admin interface can be customized to better fit your needs. For example, you can customize the way models are displayed, add search functionality, and filter options:

from django.contrib import admin
from .models import MyModel

class MyModelAdmin(admin.ModelAdmin):
    list_display = ('field1', 'field2', 'field3')  # Fields to display in the list view
    search_fields = ('field1', 'field2')  # Fields to include in the search box
    list_filter = ('field3', 'field4')  # Fields to filter by

admin.site.register(MyModel, MyModelAdmin)

In this example:

Admin Actions

You can also add custom actions to the admin interface. Admin actions allow you to perform custom tasks on multiple records at once:

from django.contrib import admin
from .models import MyModel

def make_published(modeladmin, request, queryset):
    queryset.update(status='published')

make_published.short_description = "Mark selected items as published"

class MyModelAdmin(admin.ModelAdmin):
    actions = [make_published]

admin.site.register(MyModel, MyModelAdmin)