搜尋
首頁後端開發Python教學Djangoadminsite(三)InlineModelAdmin

InlineModelAdmin

class InlineModelAdmin
class TabularInline
class StackedInline

舉例,有兩個Model:

from django.db import mod class. Field(max_length=100)

class Book(models.Model):
  author = models.ForeignKey(Author)
  title = models.CharField(max_length=100)頁


如果想在Authorlengthromdm 集

class BookInline(admin.TabularInline):

   model = Book


class AuthorAdmin(admin.ModelAdmin):
   inlines = [
 提供了兩個InlineModelAdmin的子類別:

TabularInline
StackedInline
差別在於使用的範本。
InlineModelAdmin和ModelAdmin共通的options有:

form

fieldsets

fields

formfield_overspops
form
fieldsets

fields

formfield_overss_p _fields

get_queryset()

radio_fields

readonly_fields

raw_id_fields
formfield_for_choice_field()
formfield_for_foreignkey ()
formfield_for_manytomany()
has_add_permission()
has_change_permission()
has_delete_permission()

額外增加的options有:

語,ModelAdmin.Indelline,必需的使用。

InlineModelAdmin.fk_name
model的name,當有多個外鍵時使用。

InlineModelAdmin.formset
缺省BaseInlineFormSet。

InlineModelAdmin.form

缺省ModelForm。當建立formset時傳遞給inlineformset_factory()。

InlineModelAdmin.extra

inline的額外數目。

InlineModelAdmin.get_extra()也傳回inline的額外數目。


InlineModelAdmin.max_num

可展示得最大數目。


InlineModelAdmin.get_max_num()也傳回此數字。

InlineModelAdmin.min_num
可展示的最小數目。

InlineModelAdmin.get_min_num()也傳回此數目。

InlineModelAdmin.raw_id_fields

同ModelAdmin。

class BookInline(admin.TabularInline):
   model = Book

   raw_id_fields = ("pages",)

InlineModelAdmin.template
所使用的模板。

InlineModelAdmin.verbose_name

覆蓋meta class中的verbose_name。


InlineModelAdmin.verbose_name_plural

同上


InlineModelAdmin.can_delete
默認為True。

InlineModelAdmin.get_formset(request, obj=None, **kwargs)

參考ModelAdmin.get_formsets_with_inlines.

InlineModelAdmin.get_extra(request, obj​​ans.   model = BinaryTree


   def get_extra(self, request, obj=None, **kwargs):

       extra = 2

 set.count()
       return extra


InlineModelAdmin.get_max_num(request , obj=None, **kwargs)

class BinaryTreeAdmin(admin.TabularInline):
   model = BinaryTree

   def get_max_num(self, request, obwar ):

       if obj.parent :

           return max_num - 5
       return max_num


InlineModelAdmin.get_min_num(request, objgs.

多個ForeignKey鏈向同一個Model的情況

如果有多個foreign keys:

from django.db import models

class Friendship(Fmodels.Model):
"friends")
   from_person = models.ForeignKey(Person, related_name="from_friends")


展示其中一個:

from django.contrib
展示其中一個:

from django.contrib import adminmyship. Inline) :
   model = Friendship

   fk_name = "to_person"

class PersonAdmin(admin.ModelAdmin):

   inlines = [

   orking with many-to-many models


model範例:

from django .db import models

class Person(models.Model):

   name = models.CharField(max_length=128)

class Group(models.Model):🠎 models .ManyToManyField(Person, related_name='groups')



inlines展示:

from django.contrib import admin

class MembershipInline(admin.TabularInline):
   model = Group.members.through
shipInline,
   ]

class GroupAdmin(admin .ModelAdmin):
   inlines = [
       MembershipInline,
   ]
   exclude = ('members',)

   exclude = ('members',)

The through attribute指向管理the many- to-many關係的資料庫. 

第二,, the GroupAdmin 必須排除the members欄位.

Working with many-to-many intermediary models

語 djmodel

class Person(models.Model):

   name = models.CharField(max_length=128)

class Group(models.Model):

   name = models.Fielield(max_length Person, through='Membership')


class Membership(models.Model):
   person = models.ForeignKey(Person)
   group = models.ForeignKey(Group)
  date_Key) models.CharField(max_length =64)


第一步:

class MembershipInline(admin.TabularInline):
   model = Membership
   extra = 1
lines = (MembershipInline ,)

class GroupAdmin(admin.ModelAdmin):

   inlines = (MembershipInline,)

第三步:

admin.site..Adm,

Using generic relations as an inline

一個inline with generically related objects範例:


from django.db import models
from django.contrib.contentcontentsfields Keyl.   image = models.ImageField(upload_to="images")
   content_type = models.ForeignKey(ContentType)
   object_id = models.PositiveIntegerField()
 object_id = models.PositiveIntegerField()

 object_id = models.PositiveIntegerField()

 object_id = models.PositiveIntegerField()

 object_id = models.PositiveIntegerField()

 object_id = models.PositiveIntegerField()
 object_id = models.PositiveIntegerField()
 object_id = models.PositiveIntegerField()​​  object_id = models.PositiveIntegerField() Product(models.Model):

   name = models.CharField(max_length=100)

如果想在Product add/change頁面上編輯Image實例,可以使用GenericTabularInline或GenericStackedInline:

. import GenericTabularInline

from myproject.myapp.models import Image, Product

class ImageInline(GenericTabularInline):
   ¦       ImageInline,
   ]

admin.site .register(Product, ProductAdmin)


 以上就是Djangoadminsite(三)InlineModelAdmin的內容,更多相關內容請關注PHP中文網(www.php.cn)!


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
Python與C:學習曲線和易用性Python與C:學習曲線和易用性Apr 19, 2025 am 12:20 AM

Python更易學且易用,C 則更強大但複雜。 1.Python語法簡潔,適合初學者,動態類型和自動內存管理使其易用,但可能導致運行時錯誤。 2.C 提供低級控制和高級特性,適合高性能應用,但學習門檻高,需手動管理內存和類型安全。

Python vs. C:內存管理和控制Python vs. C:內存管理和控制Apr 19, 2025 am 12:17 AM

Python和C 在内存管理和控制方面的差异显著。1.Python使用自动内存管理,基于引用计数和垃圾回收,简化了程序员的工作。2.C 则要求手动管理内存,提供更多控制权但增加了复杂性和出错风险。选择哪种语言应基于项目需求和团队技术栈。

科學計算的Python:詳細的外觀科學計算的Python:詳細的外觀Apr 19, 2025 am 12:15 AM

Python在科學計算中的應用包括數據分析、機器學習、數值模擬和可視化。 1.Numpy提供高效的多維數組和數學函數。 2.SciPy擴展Numpy功能,提供優化和線性代數工具。 3.Pandas用於數據處理和分析。 4.Matplotlib用於生成各種圖表和可視化結果。

Python和C:找到合適的工具Python和C:找到合適的工具Apr 19, 2025 am 12:04 AM

選擇Python還是C 取決於項目需求:1)Python適合快速開發、數據科學和腳本編寫,因其簡潔語法和豐富庫;2)C 適用於需要高性能和底層控制的場景,如係統編程和遊戲開發,因其編譯型和手動內存管理。

數據科學和機器學習的Python數據科學和機器學習的PythonApr 19, 2025 am 12:02 AM

Python在數據科學和機器學習中的應用廣泛,主要依賴於其簡潔性和強大的庫生態系統。 1)Pandas用於數據處理和分析,2)Numpy提供高效的數值計算,3)Scikit-learn用於機器學習模型構建和優化,這些庫讓Python成為數據科學和機器學習的理想工具。

學習Python:2小時的每日學習是否足夠?學習Python:2小時的每日學習是否足夠?Apr 18, 2025 am 12:22 AM

每天學習Python兩個小時是否足夠?這取決於你的目標和學習方法。 1)制定清晰的學習計劃,2)選擇合適的學習資源和方法,3)動手實踐和復習鞏固,可以在這段時間內逐步掌握Python的基本知識和高級功能。

Web開發的Python:關鍵應用程序Web開發的Python:關鍵應用程序Apr 18, 2025 am 12:20 AM

Python在Web開發中的關鍵應用包括使用Django和Flask框架、API開發、數據分析與可視化、機器學習與AI、以及性能優化。 1.Django和Flask框架:Django適合快速開發複雜應用,Flask適用於小型或高度自定義項目。 2.API開發:使用Flask或DjangoRESTFramework構建RESTfulAPI。 3.數據分析與可視化:利用Python處理數據並通過Web界面展示。 4.機器學習與AI:Python用於構建智能Web應用。 5.性能優化:通過異步編程、緩存和代碼優

Python vs.C:探索性能和效率Python vs.C:探索性能和效率Apr 18, 2025 am 12:20 AM

Python在開發效率上優於C ,但C 在執行性能上更高。 1.Python的簡潔語法和豐富庫提高開發效率。 2.C 的編譯型特性和硬件控制提升執行性能。選擇時需根據項目需求權衡開發速度與執行效率。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境