This time we will customize the admin form.
Let's check the current admin form.
polls/admin.py
from django.contrib import admin
# Register your models here.
from .models import Question
admin.site.register(Question)
When you access "http://127.0.0.1:8000/admin/polls/question/5/change/", the following message will be displayed.
Modify admin.py to customize the admin form. Create a QuestionAdmin class and change the display order of question_text and pub_date. Originally it was pub_date under question_text, but in the code below it will be question_text under pub_date.
polls/admin.py
from django.contrib import admin
# Register your models here.
from .models import Question
class QuestionAdmin(admin.ModelAdmin):
fields = ['question_text', 'pub_date']
admin.site.register(Question, QuestionAdmin)
polls/admin.py
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_text']}),
('Date information', {'fields': ['pub_date']}),
]
I was able to confirm that the Question is displayed, but is it not possible to display the Choice associated with the Question at the same time? We will proceed with the tutorial.
polls/admin.py
from django.contrib import admin
from .models import Question, Choice
admin.site.register(Choice)
Choice has been added. Questions associated with option "The sky" cannot be confirmed without opening the option. You can see that the option "The sky" is linked to the question "What's this?".
Shows questions and choices at the same time. ChoiceInline specifies that it wants to display three blank Choice fields.
polls/admin.py
from django.contrib import admin
# Register your models here.
from .models import Question, Choice
class ChoiceInline(admin.StackedInline):
model = Choice
extra = 3
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_text']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
inlines = [ChoiceInline]
admin.site.register(Question, QuestionAdmin)
The question "What's this?" Has choices "Not much" and "The sky" with three blank choice fields. 。
Since the Choice display is vertically long, let's modify it so that it is displayed in table format.
polls/admin.py
class ChoiceInline(admin.TabularInline):
It is displayed in a table format and is neat.
The change list is the content displayed at "http://127.0.0.1:8000/admin/
polls/admin.py
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_text']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
inlines = [ChoiceInline]
list_display = ('question_text', 'pub_date', 'was_published_recently')
Open "http://127.0.0.1:8000/admin/polls/question/". Until now, only the "QUESTION TEXT" column was available, but new "DATA PUBLISHED" and "PUBLISHED RECENTLY?" Columns have been added.
The "QUESTION TEXT" and "DATA PUBLISHED" columns support sorting. On the other hand, the "PUBLISHED RECENTLY?" Column does not support sorting, because it displays the return value of the method.
Let's use filter to narrow down by "PUBLISHED RECENTLY?".
polls/models.py
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('data published')
def __str__(self):
return self.question_text
def was_published_recently(self):
return timezone.now() - datetime.timedelta(days=1) <= self.pub_date <= timezone.now()
was_published_recently.admin_order_field = 'pub_date'
was_published_recently.boolean = True
was_published_recently.short_description = 'Published recently?'
Add list_filter.
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_text']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
inlines = [ChoiceInline]
list_display = ('question_text', 'pub_date', 'was_published_recently')
list_filter = ['pub_date']
The filter is displayed in the sidebar. You can filter by "Any date", "Today", "This week (" Past 7 days ")", "This month", "This year" I will.
Next, create a search window.
polls/admin.py
search_fields = ['question_text']
A search window was displayed at the top of the screen.
This is the end. Thank you very much.
Recommended Posts