1

Im having a problem with Django 2.1 and on_delete mandatory field.

I have an ORM like this:

class Slideshow(models.Model):
   bla bla
   dashboards = models.ManyToManyField(
        Dashboard,
        through='SlideshowDashboard',
        through_fields=('slideshow','dashboard')
        )

class SlideshowDashboard(models.Model):
    dashboard = models.ForeignKey(Dashboard, on_delete = CASCADE)
    slideshow = models.ForeignKey(Slideshow, on_delete = CASCADE)
    slide_time_dashboard = models.DurationField(null = True, blank = True)
    slide_order = models.IntegerField(null = True, blank = True)

Dashboard is another Model.

When updating the information about Slideshow, I need to TRUNCATE all previous Dashboards associated to Slideshow to save the new list.

So I use:

slideshow_to_save.dashboards.all().delete()

Unfortunally, and I don't know why, this command delete the Dashboard related entry in Dashboard table.

By specific, on_delete should delete SlideshowDashboard if I delete a Dashboard, not the opposite.

EviSvil
  • 510
  • 3
  • 21
  • 2
    It sounds like you are looking for `slideshow_to_save.dashboards.clear()`. In your current code, `slideshow_to_save.dashboards.all()` returns a queryset of all the related dashboards, so adding `.delete()` os going to remove those dashboards. – Alasdair Nov 14 '18 at 14:18

0 Answers0