Reputation: 379
How does one delete individual files in Django?
Say we have a model ('Car') and some related images ('Photos')
If multiple images are uploaded for a car, how does one delete a particular image?
Class Car(models.Model):
make = models.CharFIeld(max_length=10)
thumbnail = models.ImageField(upload_to='thumbnails/')
Class Photos(models.Model):
car = models.ForeignKey(Car, default=None)
image = models.ImageField(upload_to='images/')
We could get all of the images for the car with a query like this:
(queryset=Photos.objects.filter(car_id = pk))
but what if we only want to delete a single image - how do we get the image?
Upvotes: 0
Views: 86
Reputation: 379
It is possible to write a custom function allows you to delete individual images. This may not be the best option but it works for me.
Upvotes: 0
Reputation: 6009
Iterate over a forloop.
queryset=Photos.objects.filter(car_id = pk)
for img in queryset:
img.delete()
or if you want to delete first image, then;
queryset[0].delete()
Upvotes: 1