2012-11-05 35 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