ModelAdmin methods
save_model(request, obj, form, change)
此方法為admin介面使用者保存model實例時的行為。 request為HttPRequest實例,obj為model實例,form為ModelForm實例,change為bool值,取決於model實例是新增的還是修改的。
重寫此方法可以做一些pre-save或post-save行為。
例如,可以把request.user儲存為model實例的屬性:
from django.contrib import admin
class ArticleAdmin(admin.ModelAdmin):
sel def save_obdel(admin.ModelAdmin):
sel , def save_obdel(obofee, self,ob obj.user = request.user
obj.save()
delete_model(request, obj)
admin介面使用者刪除model實例時的方法。
save_formset(request, form, formset, change)
admin介面使用者保存formset的方法,可以改寫:
class ArticleAdmin(admin.ModelAdmin):
def save_formset(
instances = formset.save(commit=False)
for instance in instances:
instance in instances:
formset.save_m2m()
get_ordering(request)
排序。
class PersonAdmin(admin.ModelAdmin):
def get_ordering(self, request):
if request.user else:
return ['name']
get_search_results(request, queryset, search_term)
可以自訂查詢結果。
save_related(request, form, formsets, change)
formsets是model的inline formsets的列表。 model的相關物件保存時的行為。
def save_related(self, request, form, formsets, change):
""" Given the ``HttpRequest` Mode) of inline formsets and a boolean value based on whether the
parent is being added or changed, save the related objects to the
database。
"""
form.save_m2m()
for formset in formsets:
self.save_formset(request, form, formset, change=change)
get_readonly_fields(request, obj=None)
get_readonly_fields(request, obj=None)
返回fields。
get_fieldsets(request, obj=None)
回傳fieldsets。
get_list_filter(request)
回傳list_filter。
get_search_fields(request)
回傳search_fields。
get_inline_instances(request, obj=None)
回傳InlineModelAdmin物件的清單或元群組
class MyModelAdmin(admin.ModelAdmin):
sel inline(self.model , self.admin_site) for inline in self.inlines]
get_urls()
傳回ModelAdmin的可用urls。
def get_urls(self):
urls = super(MyModelAdmin, self).get_urls(url] (r'^my_view/$', self. my_view)
return my_urls + urls
def my_view(self, request):
Š pass
上面my_view方法的路徑就是 /admin/myapp/mymodel/my_view/ 。
不過上面的例子中無驗證和緩存,要提供驗證和緩存:
class MyModelAdmin(admin.ModelAdmin):
def get_urls(self):
urls = super(MyModelAdmin, MyModelAd (r'^my_view/$', self.admin_site .admin_view(self.my_view))
)
return my_urls + urls
如果頁面可緩存而還是要進行權限驗證:
(r'^my_view/$', self.admin_site.admin_view(self.my_view, cacheable=True))
get_form(request, obj=None, **kwargs)
回傳add和change view所使用的ModelForm。下面使用者非superuser時會隱藏一些欄位。
class MyModelAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs): self.exclude.append( 'field_to_hide')
return super(MyModelAdmin, self).get_form(request, obj, **kwargs)
get_formsets(request, objline)
如果只想在the change view中展示一個特殊的inline:
class MyModelAdmin(admin.ModelAdmin):
inlines = [MyInline, SomeOtherInline]sel self .get_inline_instances(request, obj):
# hide MyInline in the add view if isinstance(inline, Myinsta) and if isinstance(inline, Myinsta continue
yield inline.get_formset(request, obj)
get_formsets_with_inlines(request, obj= None)
Yields (FormSet, InlineModelAdmin) pairs for use in admin add and change views.
如果只想在the change view中展示一個特殊的inline:
MyInline, SomeOtherInline]
def get_formsets_with_inlines(self, request, obj=None):
for inline in the add viewif isinstance(inline, MyInline) and obj is None:
continueyield inline.get_formset(request, )、 formfield. 根據不同的使用者傳回不同的子集:
class MyModelAdmin(admin.ModelAdmin):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "car":, db filter(owner=request .user)
return super(MyModelAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
formfield_for_manytomany(db_db_fore)
formfield_for_manytomany(db_db_fore)
formfield_for_manytomany(db_db_foreman, rekeyd, cignman. :
class MyModelAdmin(admin.ModelAdmin):
def formfield_for_manytomany(self, db_field, request, **kwargs):
db ryset"] = Car.objects.filter (owner=request.user)
return super(MyModelAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)
def formfield_for_choice_field(self, db_field, request, **kwargs):
gs['choices'] = ( ('accepted', 'Accepted'),
('denied', 'Denied'),
. kwargs['choices'] += (('ready', 'Ready for deployment '),)
return super(MyModelAdmin, self).formfield_for_choice_field(db_field, request, **kwargs)
ModelAdmin.get_changelist(request, **kwargs)
返回changelist頁面使用的Changelis類別. 預設 django.contrib.admin.views.main.ChangeList。
ModelAdmin.get_changelist_form(request, **kwargs)
回傳changelist頁面所使用的ModelForm類別。
from django import forms
class MyForm(forms.ModelForm):
pass
class MyModelAdmin(admin.ModelAdmin):
list turn MyForm
回傳changelist頁面所使用的ModelFormSet類別。
class MyAdminFormSet(BaseModelFormSet):
pass
class MyModelAdmin(admin.ModelAdminat)quest,defobdreded kwargs['formset'] = MyAdminFormSet
return super(MyModelAdmin, self).get_changelist_formset(request, **kwargs)
ModelAdmin.has_add_permission(request)
ModelAdmin.has_add_permission(request)
是否具有權限。
ModelAdmin.has_change_permission(request, obj=None)
是否具有change權限。
ModelAdmin.has_delete_permission(request, obj=None)
是否具有delete權限。
ModelAdmin.get_queryset(request)
傳回admin介面可編輯的model QuerySet集。 根據不同的user回傳不同的結果:
class MyModelAdmin(admin.ModelAdmin):
def get_queryset(self, request):
_superuser:
return qs
return qs.filter(author=request.user)
ModelAdmin.message_user(request, messay)_lehquest, m 但是
ModelAdmin.get_paginator(queryset, per_page, orphans=0, allow_empty_first_page=True)
ModelAdmin.response_add(request, obj, post_url_continue=None)
ModelAdmin.response_change(request, obj)
ModelAdmin.response_delete(request, obj_display)
ModelAdmin.get_changeform_initial_data(request)
def get_changeform_initial_data(self, request):
< lAdmin .add_view(request, form_url='', extra_context=None)
ModelAdmin.changelist_view(request, extra_context=None)
Django view for the model instance(s) deletion confirmation page. See note below.
這5個方法是被實際的設定為django的view方法的。可以重構,一般是加入渲染view所使用的範本的context data:
class MyModelAdmin(admin.ModelAdmin):
# A template for a very customized change view:
html'
def get_osm_info(self):
# ... pass
extra_context = extra_context or {}
extra_context['osm_data '] = self.get_osm_info()
return super(MyModelAdmin, self).change_view(request, object_id,
pAdform_url_urlo_Admin) itions
為ModelAdmin的 add/change views加入js和CSS:
class ArticleAdmin(admin.ModelAdmin):
class Media:
css = {
"all": ("my_styles. js = ("my_code.js",)
Adding custom validation to the admin
客製form:
class MyArticleAdminForm(forms.ModelForm):
def clean_name(self): # do some that turnnameates y "]
class ArticleAdmin(admin.ModelAdmin) :
form = MyArticleAdminForm
以上就是Djangoadminsite(二)ModelAdminmethods的內容,更多相關內容請關注PHP中文網(www.php.cn)!