管理員的批量操作調用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