管理员的批量操作调用queryset.delete()
。
您可以覆盖查询集的.delete()
方法,即 ,因此它始终会对对象进行1x 1的删除操作。例如:
在managers.py:
from django.db import models
from django.db.models.query import QuerySet
class PhotoQueryMixin(object):
""" Methods that appear both in the manager and queryset. """
def delete(self):
# Use individual queries to the attachment is removed.
for photo in self.all():
photo.delete()
class PhotoQuerySet(PhotoQueryMixin, QuerySet):
pass
class PhotoManager(PhotoQueryMixin, models.Manager):
def get_query_set(self):
return PhotoQuerySet(self.model, using=self._db)
在models.py:
from django.db import models
class Photo(models.Model):
image = models.ImageField(upload_to='images')
objects = PhotoManager()
def delete(self, *args, **kwargs):
# Note this is a simple example. it only handles delete(),
# and not replacing images in .save()
super(Photo, self).delete(*args, **kwargs)
self.image.delete()
也许你没有删除任何东西?你能告诉我们你在调用delete()吗? – artagnon 2009-09-24 14:10:33
我试过了,只是删除管理区中的一个项目,并没有手动调用它。 – schneck 2009-09-24 14:11:35