2012-11-05 33 views
1

我目前有这个模型我如何删除徽标,而不删除公司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. 

回答

2

改变你的公司模式是这样的:

class Company(GenericUser): 
    company_name = models.CharField(max_length=150,blank = True,null = True) 
    logo = models.ForeignKey(Picture,blank = True,null = True, on_delete=models.SET_NULL) 

文档:https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.on_delete

+0

当我尝试,我得到这个错误 – user1711168

+0

你使用Django> = 1.4? on_delete选项在Django 1.4中提供,并且在以前的Django版本中不起作用。 – rafek

0

是否有任何理由:

a)您正在使用(GenericUser)?这无疑会造成你的问题。

b)不能只是删除属性(每下),然后迁移数据?

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(models.Model): 
    company_name = models.CharField(max_length=150, blank=True, null=True) 
    logo = models.ForeignKey(Picture, blank=True, null=True) 

或者是你想在这里每删除一个相关的例子: https://docs.djangoproject.com/en/dev/ref/models/relations/#django.db.models.fields.related.RelatedManager.remove

在这种情况下,你会使用remove()

+0

GenericUser是从当我删除标志公司被删除另一种模式的公司秉承太 – user1711168

+0

类型错误:__init __()得到了一个意想不到的关键字参数“on_delete” – user1711168

0

您有任何公司反对图片以清除引用对象要删除。

logo = company.logo 
company.logo = None 
logo.delete() 

但是,如果图片是由多个公司引用,试试这个:

logo = Picture.object.get(...) # the Picture you want to delete 
logo.company_set.update(logo=None) 
logo.delete() 

你也应该考虑改变公司从该参考图像,以便相关情况没有得到默认情况下删除。

class Company(GenericUser): 
    company_name = models.CharField(max_length=150,blank = True,null = True) 
    logo = models.ForeignKey(Picture,blank = True,null = True, on_delete=models.SET_NULL) 
+0

谢谢所有我试过的第一种方法,但他们没有按照我预期的方式工作,也许它的工作原理比我在展示的版本中显示的方式更复杂,但是@DanielHepper你的方法像一个魅力一样工作,感谢百万 – user1711168

+0

我如何在m2m的情况下这样做 – user1711168