Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4030374
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:27:47+00:00 2026-05-20T11:27:47+00:00

As with Django 1.2.5 a model containing a filefield will not automatically delete the

  • 0

As with Django 1.2.5 a model containing a filefield will not automatically delete the associated files any more when the model is deleted. Check the release notes here: http://docs.djangoproject.com/en/1.2/releases/1.2.5/

I am quite new to Django, so i wonder what would be a good way to preserve the old behaviour, as i have a need for it. Is it enough to just override the model.save method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:27:47+00:00Added an answer on May 20, 2026 at 11:27 am

    if you take a look at changeset 15321 in django’s code repository, you’ll see that this functionality has been removed by deleting a signal handler that the FileField had which intercepted the its parent model’s delete event and subsequently tried to delete its file.

    The functionality is quite easy to restore, you just need to “undo” these changes. One warning though: The problem with deleting files within transactions is real and if you delete files “the old fashioned way” you could end up deleting stuff even when a rollback occurs. Now, if that doesn’t pose a problem for you, read on!

    We can easy subclass the FileField and restore that functionality without touching the original class. This could should do that (note that I’m just restoring the old functionality deleted from the code):

    from django.db.models.fields.files import FileField
    
    class RetroFileField(FileField):
        # restore the old delete file when model is deleted functionality
    
        def __init__(self, verbose_name=None, name=None, upload_to='', storage=None, **kwargs):
            # init FileField normally
            super(RetroFileField, self).__init__(verbose_name, name, upload_to, storage, **kwargs)
    
        def contribute_to_class(self, cls, name):
            # restore the SIGNAL that is handled when a model is deleted
            super(RetroFileField, self).contribute_to_class(cls, name)
            signals.post_delete.connect(self.delete_file, sender=cls)
    
        def delete_file(self, instance, sender, **kwargs):
            file = getattr(instance, self.attname)
            # If no other object of this type references the file,
            # and it's not the default value for future objects,
            # delete it from the backend.
            if file and file.name != self.default and \
                not sender._default_manager.filter(**{self.name: file.name}):
                    file.delete(save=False)
            elif file:
                # Otherwise, just close the file, so it doesn't tie up resources.
                file.close()
    

    (I haven’t tested the code… but it should be more or less ok)

    You should put this code in a fields.py module in your project, or wherever it makes sense to you. Just remember, that from now on, instead of using django.db.models.FileField you’ll be using yourproject.fields.RetroFileField for your file fields. And if you’re using image fields and you depend on this functionality too… well… I think you’ll need to subclass the image fields too and make them use your RetroFileField instead of the original FileField.

    Oh, and if you don’t like the name of the field, just rename it to something more appropriate, just remember to update the super() calls inside.

    Hope this helps!

    Another note: You should see if you can just use a cron job to delete orphaned files like the changelog suggests.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I don't unterstand django-haystack properly: I have a data model containing several
As django model save() methods are not lazy , and as keeping transactions short
Every Django model has a default primary-key id created automatically. I want the model
We have a django-model containing a many-to-many field. We use the same form to
I've got a Django model containing various database model fields. One of the manager's
I have a Django model containing a text field. In the admin GUI, I'd
I have a Django model with a large number of fields and 20000+ table
Here's a Django model class I wrote. This class gets a keyerror when I
Here is a Django model I'm using. class Person(models.Model): surname = models.CharField(max_length=255, null=True, blank=True)
If a django model contains a foreign key field, and if that field is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.