程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Django learning notes - 15]: admin site editing (associated objects are added in the list page, editing page adjustment, picture setting)

編輯:Python

List of articles

  • The associated object is added in the list page
    • Display the associated objects in admin Site
      • One to one display of associated objects
      • Many to one display associated objects
  • Edit page adjustment
    • fields Set edit page
    • Group display :fieldsets
    • Related objects :Tabularlnline、Stackedlnline
      • StackedInline: Set to edit as a block
      • TabularInline: Set to edit in the form of a table
  • Picture settings

The associated object is added in the list page

Display the associated objects in admin Site

One to one display of associated objects

  1. If it is many to one or one to one , Add the association of the sub table directly to list_display in


  2. When adding from the main table , The same thing ORM Just add it directly with the lowercase of the model class as the field name

Many to one display associated objects

  1. ' many ’ Display on one side of ’ One ’ On the side of





  1. ' One ’ Display on one side of ’ many ’ On the side of

stay Wife_1 Add show_description Method , Add this method to the field



Edit page adjustment

fields Set edit page


Pay attention to models Fields that cannot be empty in must be added to the fields to be edited in the edit page

  1. null: Allow the current field to be null in the database

That is, when setting fields null=True when , stay admin When adding in the edit page , This field is required . If you want to make this field optional, you can add blank=True.

  1. Allow the current field to be in admin The editing of the site can be empty


Group display :fieldsets

fieldsets = (
(' Group 1 title ', {
'fields':[' Field 1', ' Field 2']}),
(' Group 2 title ', {
'fields':[' Field 3', ' Field 4'],
'classes':('collapse',)}) # Set whether to collapse the display 
)

Be careful :fieldsets and fields Can't exist at the same time , Choose one of the two to use




Related objects :Tabularlnline、Stackedlnline

In a one to many relationship , You can edit multiple end objects in one end of the edit page , Ways to embed multi terminal objects include tables 、 Two kinds of blocks .

  • type lnlineModelAdmin: Indicates that the edit page of the associated model is embedded in the edit page of the model
  • Subclass Tabularlnline: Embed... In the form of a table .
  • Subclass Stackedlnline: Embed... As a block .

StackedInline: Set to edit as a block

Import :from django.contrib.admin import StackedInline


TabularInline: Set to edit in the form of a table



Picture settings

  1. Add a picture field





2. Add one in admin How to display photos in


3. Cancel the escape of the picture

Modules to import :
from django.utils.safestring import mark_safe

You can do it in admin See a damaged picture in , This is because the system did not find the path of the image , So we need to configure a media The media file , Used to store pictures .

  1. media Configuration of media files
    - Create a folder in the project to store media files :media, And configure the path in the static file

    - Configure in the root route media Routing of static files

    - Add the place where the picture should be uploaded in the model class

upload_to Indicates where to put the uploaded file , I'm looking for meida instead of static


- After uploading the file again , You can see the picture , At this time, the picture is too large , We need to set the image size

  1. Set the picture size


    - Click the picture to jump to the page

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved