{{{
class MyModel(models.Model):
picture = models.ImageField(blank=True, null=True,
upload_to='pictures')
}}}
Update a single object:
{{{
>>> picture
>>> <ContentFile: Raw content>
>>> mymodel = MyModel.objects.get(pk=instance.pk)
>>> mymodel.picture = picture
>>> mymodel.save()
>>> mymodel.picture
>>> <ImageFieldFile: pictures/fbdfe25b-b246-4f2d-9436-dca49aef88d7.png>
}}}
Good. Url result
`/media/pictures/fbdfe25b-b246-4f2d-9436-dca49aef88d7.png`.
Update a single object with the `update()` method:
{{{
>>> picture
>>> <ContentFile: Raw content>
>>> MyModel.objects.filter(pk=instance.pk).update(picture=picture)
>>> mymodel.picture
>>> <ImageFieldFile: Raw content>
}}}
Bad. Url result: `/media/Raw%20content`.
--
Ticket URL: <https://code.djangoproject.com/ticket/32679>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Old description:
> My model:
>
> {{{
> class MyModel(models.Model):
> picture = models.ImageField(blank=True, null=True,
> upload_to='pictures')
>
> }}}
>
> Update a single object:
>
> {{{
> >>> picture
> >>> <ContentFile: Raw content>
> >>> mymodel = MyModel.objects.get(pk=instance.pk)
> >>> mymodel.picture = picture
> >>> mymodel.save()
> >>> mymodel.picture
> >>> <ImageFieldFile: pictures/fbdfe25b-b246-4f2d-9436-dca49aef88d7.png>
> }}}
> Good. Url result
> `/media/pictures/fbdfe25b-b246-4f2d-9436-dca49aef88d7.png`.
>
> Update a single object with the `update()` method:
>
> {{{
> >>> picture
> >>> <ContentFile: Raw content>
> >>> MyModel.objects.filter(pk=instance.pk).update(picture=picture)
> >>> mymodel.picture
> >>> <ImageFieldFile: Raw content>
>
> }}}
>
> Bad. Url result: `/media/Raw%20content`.
New description:
My model:
{{{
class MyModel(models.Model):
picture = models.ImageField(blank=True, null=True,
upload_to='pictures')
}}}
Update a single object:
{{{
>>> picture
>>> <ContentFile: Raw content>
>>> mymodel = MyModel.objects.get(pk=instance.pk)
>>> mymodel.picture = picture
>>> mymodel.save()
>>> mymodel.picture
>>> <ImageFieldFile: pictures/fbdfe25b-b246-4f2d-9436-dca49aef88d7.png>
}}}
Good. Url result
`/media/pictures/fbdfe25b-b246-4f2d-9436-dca49aef88d7.png`.
Update a single object with the `update()` method:
{{{
>>> picture
>>> <ContentFile: Raw content>
>>> MyModel.objects.filter(pk=instance.pk).update(picture=picture)
>>> mymodel = MyModel.objects.get(pk=instance.pk)
>>> mymodel.picture
>>> <ImageFieldFile: Raw content>
}}}
Bad. Url result: `/media/Raw%20content`.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/32679#comment:1>
* status: new => closed
* ui_ux: 1 => 0
* resolution: => invalid
Comment:
You cannot use `.update()` for updating a `FileField`/`ImageField`, in
that way you will update only a filename in the database. You should use
[https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.fields.files.FieldFile.save
FieldFile.save()]. If you're having trouble understanding how Django
works, see TicketClosingReasons/UseSupportChannels for ways to get help.
There is also an open ticket #29607 for adding extra examples to the
"Managing files" topic.
--
Ticket URL: <https://code.djangoproject.com/ticket/32679#comment:2>