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 6910055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:45:58+00:00 2026-05-27T08:45:58+00:00

Django 1.3 changed the behavior of models with FileField s such that when the

  • 0

Django 1.3 changed the behavior of models with FileFields such that when the instance is deleted the actual file(s) remain. For most purposes, this is fine. However, I’m testing an application that makes heavy use of ImageFields and I’m ending up with hundreds of useless leftover images in my development upload directory. Since it’s development, it’s not a huge deal, but I’d prefer to clean up after myself in the tests.

What’s the best way to make these images not hang around when testing. I emphasize that part because I don’t want to modify this behavior across the board, using a delete override, etc.

  • 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-27T08:45:59+00:00Added an answer on May 27, 2026 at 8:45 am

    I broke down and compromised a bit. I decided to go ahead and use an overridden delete, but I made deleting the actual image files dependent on passing a include_images=True kwarg.

    class Photo(models.Model):
        ...
        def delete(self, *args, **kwargs):
            if kwargs.pop('include_images', False):
                for field in self._meta.fields:
                    if type(field) == models.ImageField:
                        image = self.__getattribute__(field.name)
                        if image.name != '':
                            image.storage.delete(image.name)
            super(Photo, self).delete(*args, **kwargs)
    

    Then, I modified the tearDown method in my test cases like so:

    def tearDown(self):
        for p in Photo.objects.all():
            p.delete(include_images=True)
    

    I’m not entirely happy with this solution, but it’s the closest I could get to what I consider ideal. This should really all be handled by the tests themselves.

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

Sidebar

Related Questions

I have changed the Django models, and I use the Django schemamigration to update
I am using South with Django for database migrations. In my models.py I changed
I am writing a Django application that will track changes to the models, in
Is it possible to change the file name of an uploaded file in django?
Django 1.2 allows usernames to take the form of an email address. Changed in
I just changed my storage backend to Amazon S3, and I realized that my
I am writing a Django application that stores IP addresses with optional routing information.
I'm experiencing a really bizarre behavior when using the test client in django. I'm
I changed my Django application to use SQLAlchemy, and it works now. But I'm
Why does django-admin.py runserver restart if certain files ( urls.py )have changed, but not

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.