0

I have a django model with a field:

ImageField

When i am saving data in it, its saving successfully. But it changes its name.
Code i am using to save from django.core.files.base import ContentFile

def save_file(request):
    mymodel = MyModel.objects.get(id=1)
    file_content = ContentFile(request.FILES['video'].read())
    mymodel.video.save(request.FILES['video'].name, file_content)

Now whatever name is in

request.FILES['video'].name 

(a random 36 character uid) django just changes its last some character while saving.

Here is field definition.

file_image = models.ImageField(upload_to='some dir path', null=True)

I am using django 1.9 and python 2.7

Example input and output :

if i try to pass this id as an first argument to save it stores in db with a bit different name like this

what i gave :

123e4567-e89b-12d3-a456-426655440000

output saved in db

123e4567-e89b-12d3-a456-426655434353

75% of character remains same but end of id is getting changed

Daniyal Syed
  • 543
  • 4
  • 22
  • Can you add your ImageField definition for more information? I guess this is an parsing issue if you add any upload_to parameter on the field. – Deniz Kaplan Jun 09 '17 at 09:30
  • @DenizKaplan update question with field definition. – Daniyal Syed Jun 09 '17 at 09:33
  • Can you write an example output before and after the `mymodel.video.save()`? This is very strange, you can use a function for upload_to (https://stackoverflow.com/questions/15140942/django-imagefield-change-file-name-on-upload) if we cannot solve this issue. – Deniz Kaplan Jun 09 '17 at 09:59
  • Sorry, I cannot reproduce your case, I've created a model form and I've used form.save method and the file succesfully saved in os and DB with the correct name. I hope someone helps you for this situation. – Deniz Kaplan Jun 09 '17 at 10:43

0 Answers0