Reputation: 193
I have two models in Django, which I use for storing some document and also to build an index of the words within the documents. Here are there models:
class DMS_Dokument(models.Model):
dms_dok_titel = models.CharField(max_length=255, blank=True)
dms_dok_beschreibung = models.CharField(max_length=3000, blank=True, null=True)
dms_dok_datei = models.FileField(max_length=255,upload_to='DMS/')
dms_dok_hochgeladen_am = models.DateField()
dms_dok_indiziert = models.BooleanField(default=False)
dms_dok_gehoert_zu_app = models.CharField(max_length=255, choices=app_choices, blank=False, null=False)
dms_dok_typ = models.CharField(max_length=255, choices=typ_choices, blank=False, null=False, default='Sonstiges')
def save(self, *args, **kwargs):
preserve_ext = extension(self.dms_dok_datei.name)
neuer_dateiname = self.dms_dok_gehoert_zu_app + '_' + self.dms_dok_titel + '_' + self.dms_dok_hochgeladen_am.strftime("%d.%m.%Y")
self.dms_dok_datei.name = neuer_dateiname + preserve_ext
super(DMS_Dokument, self).save(*args, **kwargs)
class Meta:
app_label = 'DMS'
class DMS_Index_Data(models.Model):
dms_ind_wort = models.CharField(max_length=255, blank=False, null=True)
dms_ind_m2m = models.ManyToManyField(DMS_Dokument, related_name='indexes')
#dms_ind_dok_fk = models.ForeignKey('DMS_Dokument', on_delete=models.CASCADE)
class Meta:
app_label = 'DMS'
ordering = ['dms_ind_wort']
When I now use a DeleteView to delete a DMS_Dokument object, then this is correctly removed from the database. But the entries in the other table remain. How can I tell Django that upon removing a document, the index should also be cleaned from entries relating to it? And is this special, as I have many-to-many relationships?
Upvotes: 0
Views: 328
Reputation: 4208
You can override the delete()
method on your models the same way you override the save()
method.
Overriding the delete()
method is a good idea if you want the m2m data to be deleted every time with no exception.
class DMS_Dokument(models.Model):
...
def save(self, *args, **kwargs):
preserve_ext = extension(self.dms_dok_datei.name)
neuer_dateiname = self.dms_dok_gehoert_zu_app + '_' + self.dms_dok_titel + '_' + self.dms_dok_hochgeladen_am.strftime("%d.%m.%Y")
self.dms_dok_datei.name = neuer_dateiname + preserve_ext
super(DMS_Dokument, self).save(*args, **kwargs)
def delete(self):
self.indexes.all().delete()
super(DMS_Dokument, self).delete()
class Meta:
app_label = 'DMS'
You can also just override the delete()
method for your DeleteViewe
:
from django.shortcuts import get_object_or_404
def delete(self, request, *args, **kwargs):
# get the DMS_Dokument pk. you might need to change this depending on your view
instance_pk = self.kwargs['pk']
instance = get_object_or_404(DMS_Dokument, pk=instance_pk)
# delete the m2m first
instance.indexes.all().delete()
# delete the instance itself
instance.delete()
Upvotes: 1