47π
I figured it out. I just put this on that Widget model:
def delete(self):
files = WidgetFile.objects.filter(widget=self)
if files:
for file in files:
file.delete()
super(Widget, self).delete()
This triggered the necessary delete() method on each of the related objects, thus triggering my custom file deleting code. Itβs more database expensive yes, but when youβre trying to delete files on a hard drive anyway, itβs not such a big expense to hit the db a few extra times.
81π
Iβm doing the same thing and noticed a nugget in the Django docs that you should think about.
Overriding predefined model methods
Overriding Delete
Note that the delete() method for an object is not necessarily called when deleting objects in bulk using a QuerySet. To ensure customized delete logic gets executed, you can use pre_delete and/or post_delete signals.
This means your snippet will not always do what you want. Using Signals is a better option for dealing with deletions.
I went with the following:
import shutil
from django.db.models.signals import pre_delete
from django.dispatch import receiver
@receiver(pre_delete)
def delete_repo(sender, instance, **kwargs):
if sender == Set:
shutil.rmtree(instance.repo)
- [Django]-Multiple decorators for a view in Django: Execution order
- [Django]-Django internationalization for admin pages β translate model name and attributes
- [Django]-Order by count of a ForeignKey field?
4π
Using clear()
prior to deleting, removes all objects from the related object set.
see django-following-relationships-backward
example:
group.link_set.clear()
group.delete()
- [Django]-Django 1.9: Field clashes with the field of non-existing field in parent model
- [Django]-How to resolve "django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: foo" in Django 1.7?
- [Django]-Django: best practice for splitting up project into apps
1π
This seems only be sense-full if one Widget is connected to one WidgetFile exactly. In that case you should use a OneToOneField
from On-to-one examples:
# Delete the restaurant; the waiter should also be removed
>>> r = Restaurant.objects.get(pk=1)
>>> r.delete()
- [Django]-React Error: Target Container is not a DOM Element
- [Django]-Where does django install in ubuntu
- [Django]-How can I disable Django's csrf protection only in certain cases?
1π
Just to throw in a possible way around this problem: pre-delete signal. (Not in any way implying thereβs no actual solution.)
- [Django]-How to make Django's devserver public ? Is it generally possible?
- [Django]-Django viewset has not attribute 'get_extra_actions'
- [Django]-How can you create a non-empty CharField in Django?
1π
It should look like described on the django site:
class Blog(models.Model):
name = models.CharField(max_length=100)
tagline = models.TextField()
def save(self, *args, **kwargs):
do_something()
super(Blog, self).save(*args, **kwargs) # Call the "real" save() method.
do_something_else()
http://docs.djangoproject.com/en/dev/topics/db/models/#overriding-predefined-model-methods
you forgot to pass some arguments
- [Django]-Django: get table name of a model in the model manager?
- [Django]-IOS app with Django
- [Django]-What is the best way to upload files in a modern browser
0π
Is some_widget_instance
and instance of Widget
or of WidgetFile
? Because if it is an instance of Widget
it wonβt get your custom delete()
function, which is in the WidgetFile
class.
- [Django]-How can I obtain the model's name or the content type of a Django object?
- [Django]-How to send email via Django?
- [Django]-Django authentication and Ajax β URLs that require login
0π
I refactored this answer from the top answer above
import shutil
from django.db.models.signals import pre_delete
from django.dispatch import receiver
"""WidgetFile is the model for the widget files.
pre_delete means delete run this function before the object is deleted from database"""
@receiver(pre_delete, sender=WidgetFile)
def delete_preo(sender, instance, **kwargs):
shutil.rmtree(instance.repo)
- [Django]-How to add multiple arguments to my custom template filter in a django template?
- [Django]-Row level permissions in django
- [Django]-Can I make list_filter in django admin to only show referenced ForeignKeys?
-2π
From Django 1.9, if You would just define on_delete=models.CASCADE
for field, it will remove all related objects on delete.
- [Django]-Django submit two different forms with one submit button
- [Django]-Django Datefield to Unix timestamp
- [Django]-Django: what is the difference (rel & field)