0

Say I have a Car model and a CarImage model and the CarImage model uses a foreign key to relate to a Car object. If I set on_delete=models.CASCADE when I delete an image will that also delete the Car object that CarImage is linked to?

models.py

class Car(models.Model): 
    title = models.CharField(max_length=80)


class CarImage(models.Model):
    image = models.ImageField(upload_to='images')
    model = models.ForeignKey(default=1, on_delete=models.CASCADE, to='main.Car')
GTA.sprx
  • 817
  • 1
  • 8
  • 24
  • 1
    When you delete CarImage instance, Car instance linked to it still will exist. When you delete Car instance, all CarImages connected to it will be deleted. – ImustAdmit May 15 '20 at 11:20
  • Does this answer your question? [what does on\_delete do on Django models?](https://stackoverflow.com/questions/38388423/what-does-on-delete-do-on-django-models) – Bernardo Duarte May 15 '20 at 13:35

0 Answers0