# models.FileField.path does not give correct file path

**URL:** <https://forum.djangoproject.com/t/models-filefield-path-does-not-give-correct-file-path/7533>\
**Category:** Using Django\
**Created:** [April 14, 2021, 10:22am UTC](https://forum.djangoproject.com/t/models-filefield-path-does-not-give-correct-file-path/7533 "2021-04-14T10:22:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![devspectre](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/devspectre/32/3559_2.png) [@devspectre](https://forum.djangoproject.com/u/devspectre)\
**Post date:** [April 14, 2021, 10:22am UTC](https://forum.djangoproject.com/t/models-filefield-path-does-not-give-correct-file-path/7533/1 "2021-04-14T10:22:24Z")

</div>

I have a model and it has a filefield to store background image for the object.

```auto
class MyModel(models.Model, ImageResizeMixin):
	...
	...
	background = models.FileField(upload_to='backgrounds/', blank=True, null=True)
	...

	def save(self, *args, **kwargs):
		...
		...
		if self.background:
			print(self.background.path)
			self.resize(self.background, self.DEFAULT_BACKGROUND_SIZE)

# output
/srv/media_root/image.png

# actual path is /srv/media_root/backgrounds/image.png

```

On model’s `save()` method I resize the image, so it outputs a new file.  
I no need original file after conversion so I want to delete it.  
The problem is when I try to get path of the file, it does not give correct path.

```auto
print(self.background.path)

```

It outputs `/srv/media_root/image.png`  
But the actual image path is `/srv/media_root/backgrounds/image.png`  
I am not sure why `FieldFile.path` gives wrong path.  
Folder name specified by `upload_to` argument is missing.

Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 14, 2021, 1:31pm UTC](https://forum.djangoproject.com/t/models-filefield-path-does-not-give-correct-file-path/7533/2 "2021-04-14T13:31:18Z")

</div>

I believe the `path` attribute is not valid until you’ve saved the file. (I don’t think the final physical path can be known until after the storage backend has handled it, which occurs during the save.) So my understanding is that you need to call `super().save(...)` to save the file first, then you can get the physical storage path.

---

<div class="post-metadata">

**Author:** ![devspectre](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/devspectre/32/3559_2.png) [@devspectre](https://forum.djangoproject.com/u/devspectre)\
**Post date:** [April 14, 2021, 1:42pm UTC](https://forum.djangoproject.com/t/models-filefield-path-does-not-give-correct-file-path/7533/3 "2021-04-14T13:42:55Z")

</div>

Hi @KenWhitesell  
But let’s say that I saved an image already and I want to replace it.  
So I uploaded a new file on django admin and in this case, I should delete previous file because we no longer need that file  
I am not deleting current file.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 14, 2021, 2:08pm UTC](https://forum.djangoproject.com/t/models-filefield-path-does-not-give-correct-file-path/7533/4 "2021-04-14T14:08:17Z")

</div>

If it’s a field for a file that has already been saved, then the path attribute should be good. But you’d want to access the existing instance from the database, not the instance created from binding the form.  
(Actually, I don’t know off-hand if the Admin form makes the original data available - you might be able to save a query.)

---

<div class="post-metadata">

**Author:** ![devspectre](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/devspectre/32/3559_2.png) [@devspectre](https://forum.djangoproject.com/u/devspectre)\
**Post date:** [April 14, 2021, 4:05pm UTC](https://forum.djangoproject.com/t/models-filefield-path-does-not-give-correct-file-path/7533/5 "2021-04-14T16:05:06Z")

</div>

@KenWhitesell  
I finally found the solution.  
So on the save method, the self represents new object being updated or created.  
So it has new image as you said.  
So I had to fetch original object from db and got the correct path of previous file  
Thanks

```auto
def save(self, *args, **kwargs):
        ...

        this_object = Machine.objects.get(pk=self.pk)
        old_file_name = this_object.background.path
        ...

```
