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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:33:34+00:00 2026-05-27T04:33:34+00:00

Imagine a model like this: class CFile(models.Model): filepath = models.FileField(upload_to=…) collection = models.ForeignKey(FileCollection,null=True) …

  • 0

Imagine a model like this:

class CFile(models.Model):
   filepath   = models.FileField(upload_to=...)
   collection = models.ForeignKey("FileCollection",null=True)
   ... # other attributes that are not relevant

   def clean(self):
     bname = os.path.basename
     if self.collection:
       cfiles = self.baseline.attachment_set.all()
       with_same_basename = filter(lambda e: bname(e.filepath.path) == bname(self.filepath.path),cfiles)
       if len(with_same_basename) > 0:
         raise ValidationError("There already exists a file with the same name in this collection")   

class FileCollection(models.Model):
  name = models.CharField(max_length=255)
  files= models.ManyToManyField("CFile")

I want to disallow the upload of a CFile if there already exists a CFile with the same basename, that’s why I added the clean. The problem is:

  • I upload a CFile, with the name file1.png -> gets uploaded because no other files with this name exist
  • I upload another CFile, with the name file1.png -> I get the expected error that I already have a file with this name. So, I try to change the file, and upload a file with a different name ( file2.png ). The problem is, I stopped via pdb in the clean, and the model instance is still file1.png. I imagine this happens because of my ValidationError and django allows me to “correct my mistake”. The problem is I cannot correct it if I cannot upload another file. How can I handle this?

EDIT: This happens in the admin area, sorry for forgetting to mention this before. I don’t have anything custom ( besides inlines = [ FileInline ] ).

  • 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-27T04:33:35+00:00Added an answer on May 27, 2026 at 4:33 am

    I think the clearest way is to declare another field in your model for filename and make it unique for every collection. Like this:

    class CFile(models.Model):
       filepath   = models.FileField(upload_to=...)
       collection = models.ForeignKey("FileCollection",null=True, related_name='files')
       filename = models.CharField(max_length=255)
       ... # other attributes that are not relevant
    
        class Meta:
            unique_together = (('filename', 'collection'),)
    
        def save(self, *args, **kwargs):
            self.filename = bname(self.filepath.path)
            super(CFile, self).save(args, kwargs)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine this model: class ExercisePart(models.Model): exercise = models.ForeignKey(Exercise) sequence = models.IntegerField() class Meta: unique_together
My django model looks like this: class Entity(models.Model): name = models.CharField(max_length=40) examples = models.ManyToManyField(Example,
Imagine a model structure as follows: models/cross_sell_promotion.rb class CrossSellPromotion < Promotion has_and_belongs_to_many :afflicted_products, :join_table
Hi (sorry for my bad english :p) Imagine these models : class Fruit(models.Model): #
So, imagine I have this model: class Car has_one :engine end and the engine
Im playing a little bit with heavy-client app. Imagine I have this model: class
I have a problem. Imagine this data model: [Person] table has: PersonId, Name1 [Tag]
excuse me for my ugly english) ! Imagine these very simple models : class
I'm getting started with django and I'd like to extend the basic django.contrib.auth.models.User class
I have the following model set up: class UserProfile(models.Model): Additional attributes for users. url

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.