del vs delete()

时间:2012-09-19 21:04:08

标签: django object

class myClass (models.Model):
  related_operation = models.ForeignKey('self', null = True)

  __related_operation = None

  def save(self, force_insert=False, force_update=False):
    if self.related_operation != self.__related_operation:
      del self.__related_operation

    super(Operation, self).save(force_insert, force_update)
    self.__related_operation = self.related_operation

我应该使用del self .__ related_operation还是应该使用self .__ related_operation.delete()?

1 个答案:

答案 0 :(得分:1)

del self.__related_operation将尝试删除实例的__related_operation属性。我不确定你为什么要这样做。

如果要从数据库中删除对象,请使用delete()方法。