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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:43:57+00:00 2026-05-30T23:43:57+00:00

I have model UserProfile with field avatar = models.ImageField(upload_to=upload_avatar) upload_avatar function names image file

  • 0

I have model UserProfile with field avatar = models.ImageField(upload_to=upload_avatar)

upload_avatar function names image file according user.id (12.png for example).

But when user updates the avatar, new avatar name coincide with old avatar name and Django adds suffix to file name (12-1.png for example).

There are way to overwrite file instead of create new file?

  • 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-30T23:43:58+00:00Added an answer on May 30, 2026 at 11:43 pm

    Yeah, this has come up for me, too. Here’s what I’ve done.

    Model:

    from app.storage import OverwriteStorage
    
    class Thing(models.Model):
        image = models.ImageField(max_length=SOME_CONST, storage=OverwriteStorage(), upload_to=image_path)
    

    Also defined in models.py:

    def image_path(instance, filename):
        return os.path.join('some_dir', str(instance.some_identifier), 'filename.ext')
    

    In a separate file, storage.py:

    from django.core.files.storage import FileSystemStorage
    from django.conf import settings
    import os
    
    class OverwriteStorage(FileSystemStorage):
    
        def get_available_name(self, name):
            """Returns a filename that's free on the target storage system, and
            available for new content to be written to.
    
            Found at http://djangosnippets.org/snippets/976/
    
            This file storage solves overwrite on upload problem. Another
            proposed solution was to override the save method on the model
            like so (from https://code.djangoproject.com/ticket/11663):
    
            def save(self, *args, **kwargs):
                try:
                    this = MyModelName.objects.get(id=self.id)
                    if this.MyImageFieldName != self.MyImageFieldName:
                        this.MyImageFieldName.delete()
                except: pass
                super(MyModelName, self).save(*args, **kwargs)
            """
            # If the filename already exists, remove it as if it was a true file system
            if self.exists(name):
                os.remove(os.path.join(settings.MEDIA_ROOT, name))
            return name
    

    Obviously, these are sample values here, but overall this works well for me and this should be pretty straightforward to modify as necessary.

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

Sidebar

Related Questions

I have the following models: class UserProfile(models.Model): user = models.OneToOneField(User) score = models.PositiveIntegerField() class
I have the following two models: class Position(models.Model): position = models.CharField(max_length=100) class UserProfile(models.Model): user
I have user profile model with M2M field class Account(models.Model): ... friends = models.ManyToManyField('self',
I have django model: class UserProfile(models.Model): user = models.OneToOneField(User) #other fields When I create
I have two models Profile and Avatar. models.py class Profile(models.Model): user = models.ForeignKey(User) profile_name
In my application I have an extended User model called UserProfile. This user can
I have the following model set up: class UserProfile(models.Model): Additional attributes for users. url
I have a model with a DateField : class Info(models.Model): userprofile = models.OneToOneField(UserProfile, primary_key=True)
I have a field on a model that's marked unique: uid = models.CharField(max_length=255, blank=False,
I have model Foo which has field bar. The bar field should be unique,

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.