admin.site.register(models.Publisher)
admin.site.register(models.Book)
admin.site.register(models.Author)
上面的註冊方式比較簡單,若是想要進行更多的定製操做,須要利用ModelAdmin進行操做,如:javascript
# 註冊方式1 class PublisherAdmin(admin.ModelAdmin): list_display = ("name", "address") admin.site.register(models.Publisher, PublisherAdmin) # 註冊方式2 @admin.register(models.Book) class BookAdmin(admin.ModelAdmin): list_display = ("title", "price", "publish_date", "publisher")
ModelAdmin中提供了大量的可定製功能,如html
@admin.register(models.UserInfo) class UserAdmin(admin.ModelAdmin): list_display = ('user', 'pwd', 'xxxxx') def xxxxx(self, obj): return "xxxxx"
@admin.register(models.UserInfo) class UserAdmin(admin.ModelAdmin): list_display = ('user', 'pwd', 'xxxxx') list_display_links = ('pwd',)
@admin.register(models.UserInfo) class UserAdmin(admin.ModelAdmin): list_display = ('user', 'pwd','ug',) list_editable = ('ug',)
@admin.register(models.UserInfo) class UserAdmin(admin.ModelAdmin): search_fields = ('user', 'pwd')
@admin.register(models.UserInfo) class UserAdmin(admin.ModelAdmin): date_hierarchy = 'ctime'
class UserInfoInline(admin.StackedInline): # TabularInline extra = 0 model = models.UserInfo class GroupAdminMode(admin.ModelAdmin): list_display = ('id', 'title',) inlines = [UserInfoInline, ]
@admin.register(models.UserInfo) class UserAdmin(admin.ModelAdmin): # 定製Action行爲具體方法 def func(self, request, queryset): print(self, request, queryset) print(request.POST.getlist('_selected_action')) func.short_description = "中文顯示自定義Actions" actions = [func, ] # Action選項都是在頁面上方顯示 actions_on_top = True # Action選項都是在頁面下方顯示 actions_on_bottom = False # 是否顯示選擇個數 actions_selection_counter = True
add_form_template = None change_form_template = None change_list_template = None delete_confirmation_template = None delete_selected_confirmation_template = None object_history_template = None
@admin.register(models.UserInfo) class UserAdmin(admin.ModelAdmin): raw_id_fields = ('FK字段', 'M2M字段',)
@admin.register(models.UserInfo) class UserAdmin(admin.ModelAdmin): fields = ('user',)
@admin.register(models.UserInfo) class UserAdmin(admin.ModelAdmin): exclude = ('user',)
@admin.register(models.UserInfo) class UserAdmin(admin.ModelAdmin): readonly_fields = ('user',)
@admin.register(models.UserInfo) class UserAdmin(admin.ModelAdmin): fieldsets = ( ('基本數據', { 'fields': ('user', 'pwd', 'ctime',) }), ('其餘', { 'classes': ('collapse', 'wide', 'extrapretty'), # 'collapse','wide', 'extrapretty' 'fields': ('user', 'pwd'), }), )
@admin.register(models.UserInfo) class UserAdmin(admin.ModelAdmin): filter_vertical = ("m2m字段",) # 或filter_horizontal = ("m2m字段",)
@admin.register(models.UserInfo) class UserAdmin(admin.ModelAdmin): ordering = ('-id',) 或 def get_ordering(self, request): return ['-id', ]
radio_fields = {"ug": admin.VERTICAL} # 或admin.HORIZONTAL
from app01 import models from django.forms import ModelForm from django.forms import fields class MyForm(ModelForm): others = fields.CharField() class Meta: model = models = models.UserInfo fields = "__all__" @admin.register(models.UserInfo) class UserAdmin(admin.ModelAdmin): form = MyForm
@admin.register(models.UserInfo) class UserAdmin(admin.ModelAdmin): empty_value_display = "列數據爲空時,默認顯示" list_display = ('user','pwd','up') def up(self,obj): return obj.user up.empty_value_display = "指定列數據爲空時,默認顯示"
from django.contrib import admin # Register your models here. from .models import * class BookInline(admin.StackedInline): # TabularInline extra = 0 model = Book class BookAdmin(admin.ModelAdmin): list_display = ("title",'publishDate', 'price',"foo","publisher") list_display_links = ('publishDate',"price") list_filter = ('price',) list_editable=("title","publisher") search_fields = ('title',) date_hierarchy = 'publishDate' preserve_filters=False def foo(self,obj): return obj.title+str(obj.price) # 定製Action行爲具體方法 def func(self, request, queryset): print(self, request, queryset) print(request.POST.getlist('_selected_action')) func.short_description = "中文顯示自定義Actions" actions = [func, ] # Action選項都是在頁面上方顯示 actions_on_top = True # Action選項都是在頁面下方顯示 actions_on_bottom = False # 是否顯示選擇個數 actions_selection_counter = True change_list_template="my_change_list_template.html" class PublishAdmin(admin.ModelAdmin): list_display = ('name', 'email',) inlines = [BookInline, ] admin.site.register(Book, BookAdmin) # 第一個參數能夠是列表 admin.site.register(Publish,PublishAdmin) admin.site.register(Author)
單例模式(Singleton Pattern)是一種經常使用的軟件設計模式,該模式的主要目的是確保某一個類只有一個實例存在。當你但願在整個系統中,某個類只能出現一個實例時,單例對象就能派上用場。java
好比,某個服務器程序的配置信息存放在一個文件中,客戶端經過一個 AppConfig 的類來讀取配置文件的信息。若是在程序運行期間,有不少地方都須要使用配置文件的內容,也就是說不少地方都須要建立 AppConfig 對象的實例,這就致使系統中存在多個 AppConfig 的實例對象,而這樣會嚴重浪費內存資源,尤爲是在配置文件內容不少的狀況下。事實上,相似 AppConfig 這樣的類,咱們但願在程序運行期間只存在一個實例對象。python
在 Python 中,能夠用多種方法來實現單例模式:數據庫
爲了使類只能出現一個實例,咱們可使用 __new__()
來控制實例的建立過程,代碼以下:django
__new__()方法用來建立實例對象設計模式
__init__()方法用來初始化實例對象瀏覽器
class Person(object): def __init__(self, name, age): self.name = name self.age = age class Singleton(object): _instance = None def __new__(cls, *args, **kwargs): print(1) if not cls._instance: cls._instance = super(Singleton, cls).__new__(cls) return cls._instance def __init__(self, name, age): print(2) self.name = name self.age = age if __name__ == '__main__': p1 = Person("alex", 9000) p2 = Person("alex", 9000) print(p1 == p2) print(id(p1), id(p2)) print("=" * 120) s1 = Singleton("alex", 9000) s2 = Singleton("alex", 9000) print(s1 == s2) print(id(s1), id(s2))
其實,Python 的模塊就是自然的單例模式,由於模塊在第一次導入時,會生成 .pyc 文件,當第二次導入時,就會直接加載 .pyc 文件,而不會再次執行模塊代碼。所以,咱們只需把相關的函數和數據定義在一個模塊中,就能夠得到一個單例對象了。服務器
若是咱們真的想要一個單例類,能夠考慮這樣作:app
class Singleton(object): def __init__(self, name, age): self.name = name self.age = age p1 = Singleton("alex", 9000)
使用下面的代碼測試一下:
from singleton import p1 print(id(p1)) print(p1.name) p1.name = "Bob" from singleton import p1 print(id(p1)) print(p1.name)
def autodiscover():
autodiscover_modules('admin', register_to=site)
#admin.py class BookAdmin(admin.ModelAdmin): list_display = ("title",'publishDate', 'price') admin.site.register(Book, BookAdmin) admin.site.register(Publish)
這裏應用的是一個單例模式,對於AdminSite類的一個單例模式,執行的每個app中的每個admin.site都是一個對象
admin.site.register(Book, BookAdmin)
admin.site.register(Publish)
class ModelAdmin(BaseModelAdmin):pass def register(self, model_or_iterable, admin_class=None, **options): if not admin_class: admin_class = ModelAdmin # Instantiate the admin class to save in the registry self._registry[model] = admin_class(model, self)
urlpatterns = [
url(r'^admin/', admin.site.urls), ]
from django.shortcuts import HttpResponse def test01(request): return HttpResponse("test01") def test02(request): return HttpResponse("test02") urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^yuan/', ([ url(r'^test01/', test01), url(r'^test02/', test02), ],None,None)), ]