[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 7-

This time we will customize the admin form.

customize 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. image.png

Modify admin.py ①

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)

image.png

Modify admin.py ②

polls/admin.py



class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question_text']}),
        ('Date information', {'fields': ['pub_date']}),
    ]

image.png

Add relational objects

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. image.png Questions associated with option "The sky" cannot be confirmed without opening the option. image.png You can see that the option "The sky" is linked to the question "What's this?". image.png

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. 。 image.png

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. image.png

Customize the changelist page on the admin site

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. image.png

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.

image.png

Next, create a search window.

polls/admin.py


search_fields = ['question_text']

A search window was displayed at the top of the screen.

image.png

This is the end. Thank you very much.

Recommended Posts

[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 7-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 1-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 2-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 0-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 5-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 6-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 4-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 3-
Deploy a Python 3.6 / Django / Postgres web app on Azure
(Python) Try to develop a web application using Django
CTF beginner tried to build a problem server (web) [Problem]
Web scraping beginner with python
[Django3] Display a web page in Django3 + WSL + Python virtual environment
A liberal arts engineer tried knocking 100 language processes in Python 02
A python beginner tried to intern at an IT company
A liberal arts engineer tried knocking 100 language processes in Python 01
A liberal arts engineer tried knocking 100 language processes in Python 00
If you know Python, you can make a web application with Django
[Python / Django] Create a web API that responds in JSON format
I tried web scraping with python.
Build a web application with Django
Python beginner tried 100 language processing knock 2015 (05 ~ 09)
web coder tried excel in Python
When a Python beginner tried using Bottle, it worked unexpectedly easily.
Python beginner tried 100 language processing knock 2015 (00 ~ 04)
A beginner of machine learning tried to predict Arima Kinen with python
An introduction to self-made Python web applications for a sluggish third-year web engineer
[Beginner] Python web scraping using Google Colaboratory
I have a question! (Python, django) Easy
Daemonize a Python web app with Supervisor
I tried a functional language with Python
[Python] A quick web application with Bottle!
I tried to make a Web API
Use Django from a local Python script
Run a Python web application with Docker
Let's make a web framework with Python! (1)
I tried benchmarking a web application framework
Let's make a web framework with Python! (2)
I made a WEB application with Django
A python beginner tried to intern at an IT company [Day 2 chatbot survey]
A python beginner tried to intern at an IT company [Day 1 development process]
Go beginner tried to create a cloud native web application using Datastore / GAE
[ES Lab] I tried to develop a WEB application with Python and Flask ②
I searched for the skills needed to become a web engineer in Python
Machine learning memo of a fledgling engineer Part 1
2-2. Input for becoming a WEB engineer (Linux basics)
How to open a web browser from python
I tried web scraping using python and selenium
Python web framework Django vs Pyramid vs Flask December 2015
I tried playing a typing game in Python
Start a simple Python web server with Docker
[Python] Build a Django development environment with Docker
[Memo] I tried a pivot table in Python
[Python] Draw a Mickey Mouse with Turtle [Beginner]
Create a web map using Python and GDAL
Steps to develop a web application in Python
[Python] Web development preparation (building a virtual environment)
I tried reading a CSV file using Python
Programming beginner Python3 engineer certification basic exam record
Launch a web server with Python and Flask
Machine learning memo of a fledgling engineer Part 2