Skip to content

Customizing Campaign Admin View

In this video, we are going to customize the admin view for campaign and slots. So, as of now, you have to manage campaigns and slots separately, but at the end of the video, you can manage campaign and slots right from the same page.

Admin Panel

So, open the admin.py file of campaign.

from django.contrib import admin
# Import the Campaign and slot Models.
from vaccination.models import Slot, Campaign

As you know that, the relationship between campaign and slots is one-to-many. That means each campign will have multiple slots and each slot can be asssociated with only one campaign.

So, what we will do, we will create a inline admin panel for slot that can be accessed right from the campaign detail view. That means, when you open a certain campaign details view from the admin panel, then you will be able to manage all the slots associated with that campaign right from the same template.

Therefore, lets create a inline admin for slot.

# At first, create a class named SlotInline and then inherit admin.TabularInline
class SlotInline(admin.TabularInline):
    # In this class, specify the model name.
    model = Slot
    # Then, I will also set some fields as readonly fields.
    readonly_fields = ["reserved"]

In this way, you have create a inline admin for slot that can be used to manage slots for each campaigns.

Now, lets create the custom admin panel for campaign.

# So, at first, create a class named CustomCampaignAdmin.
# Now, inherit the admin.ModelAdmin
class CustomCampaignAdmin(admin.ModelAdmin):
    # In this class, specify the inlines which are available for the CustomCampaignAdmin
    inlines = [SlotInline]

# At last, register Campaign with the CustomCampaignAdmin.
admin.site.register(Campaign, CustomCampaignAdmin)

[Run the development server and explain the changes]

In this way, we have created an interface where we can manage both campaign and slots from the same admin page.

Now, in the campaign list view, lets try to add a search bar where I should be able to search any vaccination camapigns by the vaccine name and center name.

search_fields = ["vaccine__name", "center__name"]

[Run the development server and explain the changes]

Now, I want to display some more information about the campaign in this list view.

list_display = ["vaccine", "center", "start_date", "end_date"]

[Run the development server and explain the changes]

Now, I want to set the ordering of this list view. I want to show campaigns ordered by the start date in decending order.

ordering = ["-start_date"]

[Run the development server and explain the changes]

At last, in the campaign detail page, I want to show start_date and end_date in the same line not on different line.

fields = (("vaccine"), ("center"), ("start_date", "end_date"), ("agents"))

[Run the development server and explain the changes]