Reputation: 707
i currently have this models how do i delete the logo without deleting the company model.Please sample codes if possible thanks.
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)
this is a model how will i then remove the foto from the land model the model looks like this.
class Land(Properies):
photo = models.ManyToManyField(Picture,blank=True,related_name='Land_Pictures',null = True)
i tried this bit it dose not work
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
i keep getting an error like that id is set to none and if i hit refresh it works i think
Exception Type: AssertionError
Exception Value:
Picture object can't be deleted because its id attribute is set to None.
Upvotes: 1
Views: 6310
Reputation: 29967
You have to clear the references from any Company objects to the Picture object you want to delete.
logo = company.logo
company.logo = None
logo.delete()
However, if a Picture is referenced by multiple Companies, try this:
logo = Picture.object.get(...) # the Picture you want to delete
logo.company_set.update(logo=None)
logo.delete()
You should also consider changing the reference from Company to Image so related instances do not get deleted by default.
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)
Upvotes: 2
Reputation: 4318
Is there any reason:
a) you are using (GenericUser)
? This will undoubtedly cause you problems.
b) can't just delete the attribute (per below) and then migrate the data?
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)
or are you trying to delete a related instance per here: https://docs.djangoproject.com/en/dev/ref/models/relations/#django.db.models.fields.related.RelatedManager.remove
In which case you'd use remove()
.
Upvotes: 0
Reputation: 635
Change your Company model this way:
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)
docs: https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.on_delete
Upvotes: 2