# Make upload\_to path of ImageField contain a CharField value of a field in the same model

**URL:** <https://forum.djangoproject.com/t/make-upload-to-path-of-imagefield-contain-a-charfield-value-of-a-field-in-the-same-model/11498>\
**Category:** Using Django\
**Created:** [January 11, 2022, 7:35am UTC](https://forum.djangoproject.com/t/make-upload-to-path-of-imagefield-contain-a-charfield-value-of-a-field-in-the-same-model/11498 "2022-01-11T07:35:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![blaketig](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/blaketig/32/6323_2.png) [@blaketig](https://forum.djangoproject.com/u/blaketig)\
**Post date:** [January 11, 2022, 7:35am UTC](https://forum.djangoproject.com/t/make-upload-to-path-of-imagefield-contain-a-charfield-value-of-a-field-in-the-same-model/11498/1 "2022-01-11T07:35:19Z")

</div>

Is it possible to define the upload\_to path of an ImageField based on a CharField in the same model? For example:

```auto
class Profile(models.Model):

	def make_image_path(pathfolder):
		image_path = f"users/{pathfolder}/"
		return image_path
		
	username = models.CharField(max_length=50, unique=True)
	photo = models.ImageField(upload_to=make_image_path(username))

```

This currently works except result is `users/<django.db.models.fields.CharField>/picture.jpeg`

How can I make it show the username instead of \<django.db.models.fields.CharField\>.

---

<div class="post-metadata">

**Author:** ![bmispelon](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/bmispelon/32/38_2.png) [@bmispelon](https://forum.djangoproject.com/u/bmispelon)\
**Post date:** [January 11, 2022, 4:04pm UTC](https://forum.djangoproject.com/t/make-upload-to-path-of-imagefield-contain-a-charfield-value-of-a-field-in-the-same-model/11498/2 "2022-01-11T16:04:48Z")

</div>

Hi,

You’re using `upload_to` incorrectly.

Because of the way Python works, the value of `upload_to` is the return value of the function `make_image_path()`. What you want is for `upload_to` to be an actual function object.

There’s an example of using a function for `upload_to` in Django’s documentation: [Model field reference | Django documentation | Django](https://docs.djangoproject.com/en/4.0/ref/models/fields/#django.db.models.FileField.upload_to) (look for the code example that defines `user_directory_path` a few paragraphs down).

I hope that helps!

---

<div class="post-metadata">

**Author:** ![blaketig](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/blaketig/32/6323_2.png) [@blaketig](https://forum.djangoproject.com/u/blaketig)\
**Post date:** [January 11, 2022, 10:15pm UTC](https://forum.djangoproject.com/t/make-upload-to-path-of-imagefield-contain-a-charfield-value-of-a-field-in-the-same-model/11498/3 "2022-01-11T22:15:25Z")

</div>

Thank you, this works.
