我目前有这个模型我如何删除徽标,而不删除公司model.Please示例代码,如果可能的话谢谢。删除相关模型django
class Picture(models.Model):
owner = models.ForeignKey(User,blank = True)
caption = models.CharField(max_length=150, blank=True, null=True)
image = ImageField(upload_to='images/',blank = True, null = True)
class Company(GenericUser):
company_name = models.CharField(max_length=150,blank = True,null = True)
logo = models.ForeignKey(Picture,blank = True,null = True)
这是一个模型,我将如何从土地模型中删除照片模型看起来像这样。
class Land(Properies):
photo = models.ManyToManyField(Picture,blank=True,related_name='Land_Pictures',null = True)
我想这一点是没有作用
checked_list = []
start = 1
land_photos = sorted(list(land.photo.select_related()),reverse =True)
while start < 8:
photo = 'photo%s' % start
checked = form.cleaned_data[photo]
if checked != None:
checked_list.append(land_photos[start - 1])
start += 1
for a_foto in checked_list:
land.photo.remove(a_foto)
try:
a_foto.remove_all_file()
a_foto.delete()
except OSError:
pass
我不断收到这样的ID错误设置为none,如果我打刷新它的作品,我认为
Exception Type: AssertionError
Exception Value:
Picture object can't be deleted because its id attribute is set to None.
当我尝试,我得到这个错误 – user1711168
你使用Django> = 1.4? on_delete选项在Django 1.4中提供,并且在以前的Django版本中不起作用。 – rafek