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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:59:19+00:00 2026-06-01T04:59:19+00:00

I have a model with a first_name and last_name field, and these are used

  • 0

I have a model with a first_name and last_name field, and these are used to create a filename on an ImageField. The argument for upload_to on the ImageField is this method that generates the filename with this instance’s information.

When this model instance is saved, would the calls to .strip() in the clean() be applied to the fields before they are used to generate the filename? Or would I need to do .strip() on the data when it’s used, as well as in the clean?

models.py:

def set_path(instance, filename):
    """
    Set the path to be used for images uploaded (trainer photos).
    """
    return u'about/%(first)s_%(last)s.%(ext)s' % {
        'first': instance.first_name.strip(' \t').lower(), #.strip() required?
        'last': instance.last_name.strip(' \t').lower(), #.strip() required?
        'ext': filename.split('.')[-1]
    }

class Trainer(models.Model):
    """
    Trainers and their associated information.
    """
    first_name = models.CharField(max_length=25)
    last_name = models.CharField(max_length=25)
    image = models.ImageField(upload_to=set_path, blank=True, null=True,
        verbose_name="Trainer image")
    description = models.TextField()

    class Meta:
        unique_together = ('first_name', 'last_name',)

    def clean(self):
        super(Trainer, self).clean()
        # Are these calls to .strip() applied before the fields
        # get used as `instance` to determine a filename?
        self.first_name = self.first_name.strip(' \t')
        self.last_name = self.last_name.strip(' \t')
        self.description = self.description.strip(' \t\r\n')
  • 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-06-01T04:59:20+00:00Added an answer on June 1, 2026 at 4:59 am

    If there is a callable for the upload_to argument, it is called during the save() method of the model base. The save() is of course called after clean(), so you do NOT need to strip() any fields if you’ve already done so in the clean() method.

    You can see where the code is called on line 90 of the Django source code: https://code.djangoproject.com/browser/django/trunk/django/db/models/fields/files.py

    generate_filename is the stored variable that points to whatever you passed into upload_to.

    So, the order is form submit -> model.full_clean() -> overridden clean() -> save(), which calls upload_to()

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

Sidebar

Related Questions

I have a model that holds user address. This model has to have first_name
I have a simple Book Author relationship class Author(models.Model): first_name = models.CharField(max_length=125) last_name =
I have a model that has two fields, which I will call first_name and
I have these two database tables: locations id name users id location_id last_name first_name
If I have the following Model: class Person(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30)
I have a model hierarchy that looks something like this: Office | +-- Person
I have a user model that has properties such as username, password, first name,
if I have a model that has two model choice fields, is there a
I have a backbone model called Member - this contains membership data such as
In Django I have this: models.py class Book(models.Model): isbn = models.CharField(max_length=16, db_index=True) title =

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.