[Answered ]-Django foreign key saving with file name not object

2👍

That is because self.media_file will give you the file object not the url to the saved file. Change your code to return the url of the file. The easiest could be –

def __str__(self):
    return self.media_file.url

Read it about here –

https://docs.djangoproject.com/en/1.8/ref/models/fields/#filefield
https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.fields.files.FieldFile.url

Leave a comment