# coding:utf-8
from __future__ import unicode_literals
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class Article(models.Model):
title = models.CharField(
'标题'
, max_length=256)
content = models.TextField(
'内容'
)
pub_date = models.DateTimeField(
'发表时间'
, auto_now_add=True, editable = True)
update_time = models.DateTimeField(
'更新时间'
,auto_now=True, null=True)
def __str__(self):
return
self.title
1.定制加载的列表, 根据不同的人显示不同的内容列表,比如输入员只能看见自己输入的,审核员能看到所有的草稿,这时候就需要重写get_queryset方法
class
MyModelAdmin(admin.ModelAdmin):
def
get_queryset(
self
, request):
qs
=
super
(MyModelAdmin,
self
).get_queryset(request)
if
request.user.is_superuser:
return
qs
else
:
return
qs.
filter
(author
=
request.user)
class
PersonAdmin(admin.ModelAdmin):
list_display
=
(
'name'
,
'age'
)
search_fields
=
(
'name'
,)
def
get_search_results(
self
, request, queryset, search_term):
queryset, use_distinct
=
super
(PersonAdmin,
self
).get_search_results(request, queryset, search_term)
try
:
search_term_as_int
=
int
(search_term)
queryset |
=
self
.model.objects.
filter
(age
=
search_term_as_int)
except
:
pass
return
queryset, use_distinct
queryset 是默认的结果,search_term 是在后台搜索的关键词
3.修改保存时的一些操作,可以检查用户,保存的内容等,比如保存时加上添加人
1
2
3
4
5
6
|
from
django.contrib
import
admin
class
ArticleAdmin(admin.ModelAdmin):
def
save_model(
self
, request, obj, form, change):
obj.user
=
request.user
obj.save()
|
其中obj是修改后的对象,form是返回的表单(修改后的),当新建一个对象时 change = False, 当修改一个对象时 change = True
如果需要获取修改前的对象的内容可以用
1
2
3
4
5
6
7
|
from
django.contrib
import
admin
class
ArticleAdmin(admin.ModelAdmin):
def
save_model(
self
, request, obj, form, change):
obj_original
=
self
.model.objects.get(pk
=
obj.pk)
obj.user
=
request.user
obj.save()
|
那么又有问题了,这里如果原来的obj不存在,也就是如果我们是新建的一个怎么办呢,这时候可以用try,except的方法尝试获取,当然更好的方法是判断一下这个对象是新建还是修改,是新建就没有 obj_original,是修改就有
1
2
3
4
5
6
7
8
9
10
11
|
from
django.contrib
import
admin
class
ArticleAdmin(admin.ModelAdmin):
def
save_model(
self
, request, obj, form, change):
if
change:
# 更改的时候
obj_original
=
self
.model.objects.get(pk
=
obj.pk)
else
:
# 新增的时候
obj_original
=
None
obj.user
=
request.user
obj.save()
|
4, 删除时做一些处理,
1
2
3
4
5
6
7
8
9
|
from
django.contrib
import
admin
class
ArticleAdmin(admin.ModelAdmin):
def
delete_model(
self
, request, obj):
"""
Given a model instance delete it from the database.
"""
# handle something here
obj.delete()
|