from django.contrib import admin from .models import Article,Category from datetime import datetime # Register your models here. @admin.register(Article) class ArticleAdmind(admin.ModelAdmin): list_display = ['id','title','add_time','state'] list_display_links = ('id', 'title') search_fields = ('title',) #定制搜索 #def get_search_results(self,request,queryset,search_term): # queryset,use_distinct = super(ArticleAdmind,self).get_search_results(request,queryset,search_term) def delete_queryset(self, request, queryset): """ Given a queryset, delete it from the database. 重新修改删除操作 """ # if queryset.count() == 1: # obj = queryset.get() # obj.state = 0 # obj.update_time = datetime.now # obj.save() # else: for obj in queryset.filter(): obj.state = 0 obj.update_time = datetime.now() obj.save() #print(dir(queryset)) #print(queryset.filter()) def save_model(self, request, obj, form, change): """ Given a model instance save it to the database. 重新写修改或者保存操作 如果是新加 obj.id 返回None """ obj.save() @admin.register(Category) class CategoryAdmin(admin.ModelAdmin): list_display = ['id','name','add_time']