我想从一个实际的obejct调用mutliple对象的方法。我可以这样做,但它并不理想,因为我想使用对象上已有的方法,这不仅仅是更新字段(loggin等):
class Category(models.Model):
name = models.CharField(max_length=200)
status = models.CharField(max_length=200)
def deactivate(self):
self.status = 'inactive'
logger.info('The category %s has been deactivated.' % (self.name))
Items.objects.filter(Category=self).update(status='inactive')
logger.info('The items in category %s have been deactivated.' % (self.name)
class Items(models.Model):
category = models.ForeignKey(Category)
name = models.CharField(max_length=200)
status = models.CharField(max_length=200)
def deactivate(self):
self.status = 'inactive'
logger.info('The item %s has been deactivated.' % (self.name))
所以我不喜欢Items.objects.filter(Category=self).update(status='inactive')
,而是Items.objects.filter(Category=self).deactivate()
。
这是否可以不循环浏览所有记录?