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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:03:29+00:00 2026-05-20T01:03:29+00:00

I have a Model containing a FileField. I’d like this FileField to have a

  • 0

I have a Model containing a FileField.
I’d like this FileField to have a unique path.

At first, at though about using the ID of the entry, but Django move the file to it’s upload_to path before saving the entry, so ID is empty.

Moreover, I can’t use something like the title or any other elements of the model (except the creation date) since they can be changed by the user. And I prefer not to copy/delete a file every time a user change the title of it’s entry (if I use the title as a part of my path).

Here start my research, I found these :

  • Generate a unique key and compare it to the database. While the key exist, we generate a new one (Django, unique field generation) : The problem is the potentials hits the while could do to the database before having a unique key

  • Getting the timestamp from the creation date. The problem here, is if two people add a file at the exact same time, it will generate conflicts

I’d like to have this unique ID a small as possible, max length of 7 would be great. The perfect solution would have been to have the ID of the entry. Do you know a workaround to do so (calling save() before moving files to their upload_to folder?) or if not, which implementation would be the best, based on one of my solutions or a one you think is better?

  • 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-20T01:03:29+00:00Added an answer on May 20, 2026 at 1:03 am

    Since FileField is, by default, with null=True, blank=True, a possibility is to save the model twice, first by removing the file value (=None), saving, and then, adding the the file value (that was previously stored in a temporary var), and save again.

    Here is the code

    # this method goes in your model
    def save(self, *args, **kwargs):
        # ignoring the double save if this is an update or if there is no files
        if self.pk or not self.file:
            return super(MyModel, self).save(*args, **kwargs)
    
        old_file = self.file._file
        self.file = None
    
        super(MyModel, self).save(*args, **kwargs)
    
        self.file = old_file
    
        return super(MyModel, self).save(*args, **kwargs)
    

    Well, of course, this will result in a double request to the database when you create a new entry, but any other solution I could come up with required to do at least one hit to the database (key based with unique constraint, key based on the creation date, etc).

    Hope this helps!

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

Sidebar

Related Questions

I have a simple model containing a FileField among others. When I use a
I have a model which looks like this: class test (db.Model) : tagList =
Using Rails 3 I have a number of model containing serialized attributes. To perform
I have a model containing a ManyToMany field to a table Tags. Since this
Let's say I'm using Spring and have a controller that returns a model containing
I have a model that looks something like this: public class SampleModel { public
I have a model containing products. I would like to create a search form
I have a model containing ImageField which should be resized after uploading. class SomeModel(models.Model):
I have an old Xcode project which contains a CoreData model (containing a version
I have a YML file containing fixtures for a Rails model (Comment) which looks

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.