DJango Memo: From the beginning (more edits to the management screen)

I will continue to play with the admin page.

Add Choice

Last time went to the point of registering Poll, but this time we will also register Choice (Review: Poll and Choice in models.py) One is defined).

You can use it as admin.site.register (Choice), but that will make the Poll and Choice registration screens separate. The relationship between the two is that Poll contains multiple Choices, so I want to create a screen that conforms to that.

admin.py ######

class ChoiceInline(admin.StackedInline):  #Is StackedInline an inline format?
    model = Choice
    extra = 3  #What does it mean to put in how many models?

class PollAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
    ]
    inlines = [ChoiceInline]  #Is inline like an additional area?

admin.site.register(Poll, PollAdmin)

#"?" Is added to the end of the comment because I am not confident in my understanding.

Choice can also be registered on the Poll edit screen. If you want to make it more compact, you can set the parent class of ChoiceInline to TabularInline and each Choice will fit in one line.

All models are now registered on the admin screen.

I'll play with it a little more.

Customize changelist page

By default, the list display screen only displays each saved data as a character string, but you can add other elements here as well.

To do this, add the following line to the PollAdmin class.

admin.py ######

class PollAdmin(admin.ModelAdmin):
    # ...
    list_display = ('question', 'pub_date')  # list_Specify display with display
    #I happened to make a mistake in writing and confirmed the operation even in list format. Is it natural

And if you write like this

    list_display = ('question', 'pub_date’,’was_published_recently’)
    # was_published_recently is a function defined in Poll (note that recently was today in the previous document)

It seems that it is also possible to display the return value from the function. By the way, the underscore is replaced with a blank.

Add options

As a preliminary step, I will add a little Poll model.

models.py ######

from django.utils import timezone

class Poll(models.Model):
    # ...
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)  #Is it within a day of being published?
    was_published_recently.admin_order_field = 'pub_date'
    was_published_recently.boolean = True
    was_published_recently.short_description = 'Published recently?'

And you can add options by adding the following to the PollAdmin class.

admin.py ######

    list_filter = ['pub_date']  # pub_Function to narrow down based on date
    search_fields = ['question']  #Ability to search based on question
    date_hierarchy = 'pub_date'  #Similar in function to date filter

It seems that you can customize it in more detail by using a template, but I will not touch it this time because I will do it in detail in the next tutorial. And

It seems that the next screen will finally be made for publication

Enter Tutorial 3. I'm looking forward.

Recommended Posts

DJango Memo: From the beginning (more edits to the management screen)
DJango memo: From the beginning (using the management screen) my addictive point
DJango Memo: From the beginning (Error screen settings)
DJango memo: From the beginning (editing the management screen) There is a mystery
DJango Memo: From the beginning (preparation)
DJango Memo: From the beginning (model settings)
DJango Memo: From the beginning (creating a view)
Log in to the fortigate (6.0) management screen from selenium-try to log out
DJango Note: From the beginning (form processing)
Django memo # 1 from scratch
DJango Note: From the beginning (using a generic view)
Transit to the update screen with the Django a tag
DJango Note: From the beginning (simplification and splitting of URLConf)
How to register the same data multiple times with one input on the Django management screen
How to do the initial setup from Django project creation
Development of WEB application using Django [Add data from management screen]
[Django] Correct the plural form of the model name on the management screen
The story of failing to update "calendar.day_abbr" on the admin screen of django
Django admin screen reverse lookup memo
Tensorflow memo [updated from time to time]
Get the value from the [Django] Form
Throw GQL with a numeric ID from the App Engine management screen
Try using the Python web framework Django (1)-From installation to server startup
How to build an application from the cloud using the Django web framework
How to pass values to JavaScript variables directly from the [Django] template tag
How to make only one data register on the Django admin screen
How to check the version of Django
Learning notes from the beginning of Python 1
Omit BOM from the beginning of the string
How to operate Linux from the console
How to access the Datastore from the outside
Learning notes from the beginning of Python 2
Get only the text from the Django form.
Redo everything for the Django login screen
[Django memo] I want to set the login user information in the form in advance
How to automatically generate API document with Django REST framework & POST from document screen