Django 백그라운드 사용자 정의 선택 상자 삭제 함수

4947 단어
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']

좋은 웹페이지 즐겨찾기