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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:01:00+00:00 2026-05-13T18:01:00+00:00

Hay, i have a model which saves 2 images class Picture(models.Model): picture = models.ImageField(upload_to=make_filename)

  • 0

Hay, i have a model which saves 2 images

class Picture(models.Model):
    picture = models.ImageField(upload_to=make_filename)
    thumbnail = models.ImageField(upload_to=make_thumb_filename)
    car = models.ForeignKey('Car')
    created_on = models.DateField(auto_now_add=True)
    updated_on = models.DateField(auto_now=True)

    def save(self):
        super(Picture, self).save()
        size = 200, 200
        filename = str(self.thumbnail.path)
        image = Image.open(filename)
        image.thumbnail(size, Image.ANTIALIAS)
        image.save(filename)

As you can see i have overwrote the save() method
I’n my view i have a simple try, except block, which checks for IOErrors (which are raised if a file other than an image is uploaded)

def upload(request):
    car = Car.objects.get(pk=1)
    try:
        picture = Picture(picture=request.FILES['image'], thumbnail=request.FILES['image'], car=car)
        picture.save()
    except IOError:
        return HttpResponseRedirect("/test/")

However, the exception is raised, but the files are still written to the server (and db)

Any ideas how to make sure files dont get written if the IOError is raised?

EDIT

Fixed by writing a custom method

def is_accectable_file(filename):
    extension = filename.split('.')[-1]
    acceptable_filetypes = ['jpeg','jpeg','gif','png']
    if extension in acceptable_filetypes:
        return True
    else:
        return False

Then exiting my model to

class Picture(models.Model):
    picture = models.ImageField(upload_to=make_filename)
    thumbnail = models.ImageField(upload_to=make_thumb_filename)
    car = models.ForeignKey('Car')
    created_on = models.DateField(auto_now_add=True)
    updated_on = models.DateField(auto_now=True)

    def save(self, *args, **kwargs):
        if is_accectable_file(self.picture.name):
            super(Picture, self).save(*args,**kwargs)
            size = 200, 200
            filename = str(self.thumbnail.path)
            image = Image.open(filename)
            image.thumbnail(size, Image.ANTIALIAS)
            image.save(filename)
            return True
        else:
            return False

and my view to

def upload(request):
    car = Car.objects.get(pk=1)
    try:
        picture = Picture(picture=request.FILES['image'], thumbnail=request.FILES['image'], car=car)
        if picture.save():
            return HttpResponse("fine")
        else:
            return HttpResponse("invalid type")
    except:
        return HttpResponse("no file")
  • 1 1 Answer
  • 1 View
  • 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-13T18:01:00+00:00Added an answer on May 13, 2026 at 6:01 pm

    The code that (I assume) throws the IOError is being run after you call the super(Picture,self).save() method. Because of this, the picture getting written to the database even if the exception is thrown.

    You just need to move the super call to after the setup code.

    As an aside, if you’re overriding save I’d recommend doing it as follows:

    def save(self,*args,**kwargs):
        ...
        super(Picture, self).save(*args,**kwargs)
    

    Otherwise you’ll get an exception in any case where Django is passing in arguments to save (and I believe there are a few cases where it does, at least in the admin).

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

Sidebar

Related Questions

Hay, I have a Model which looks like this class Person(models.Model): name = models.CharField(blank=False,
Hay, i have a model which houses a board class Board(models.Model): parent_board = models.ForeignKey('self',
Hay, i have a field in one of my models which saves the creation
Hay, i have a simple model class Manufacturer(models.Model): name = models.CharField() car_count = models.IntegerField()
Hay I have a model like this def Photo(models.Model): # Photo object fields... def
Hay i have a model def Friends(models.Model): user = models.ManyToManyField(User) def User(models.Model): and i
Hay all, i have a simple model like this def Article(models.Model): upvotes = models.ManyToManyField(User,
Hay, serializers is not returning JSON object make = Make.objects.filter(slug__exact=make) models = Model.objects.filter(make=make).values('slug','name') json_models
Hay, say i have 2 models listing and category. How would i use djangos
Hay I have an element like this <span class='a.b'> Unfortunately this class name comes

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.