제가 현재 클래스의 인스턴스가 아닌 일부 개체에서 일부 delete()
및 일부 save()
메서드를 수행하려고합니다. 시도하고 있습니다. 오버로드 된 save()
클래스의 메서드에서이 작업을 수행 할 수 있습니다.다른 클래스의 모델에서 데이터베이스의 개체를 수정할 수 있습니까
class Item(models.Model):
name = models.CharField(max_length=500)
category = models.ForeignKey(Category, null=True, related_name='add_item')
tag = models.ManyToManyField(Category, null=True, related_name='tag_item')
def save(self, *args, **kwargs):
super(Item, self).save(*args, **kwargs)
for a_tag in self.tag.all():
#do stuff here
a_tag.delete()
cat = self.category
while cat is not None:
cat.add_item.add(self)
#do stuff here
cat.save()
cat = cat.parent
이 작동하지 않습니다와 나는 다음과 같은 예외가 얻을이 수행하려고 할 때 :
Exception AttributeError: "'Cursor' object has no attribute 'connection'" in <bound method Cursor.__del__ of <MySQLdb.cursors.Cursor object at 0x905c3ac>> ignored
전체 추적을 게시하십시오. 비록 당신이하고있는 일이 가능하지 않더라도, 당신은 그 오류를 얻지 않아야한다. 그래서 이것은 장고 버그 일 수있다. –