[Django] #32679: Django model not updating image field correctly (Raw content)

50 views
Skip to first unread message

Django

unread,
Apr 23, 2021, 12:19:28 PM4/23/21
to django-...@googlegroups.com
#32679: Django model not updating image field correctly (Raw content)
-------------------------------------+-------------------------------------
Reporter: mcardozo | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 3.1
layer (models, ORM) |
Severity: Normal | Keywords: models
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 1 |
-------------------------------------+-------------------------------------
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`.

--
Ticket URL: <https://code.djangoproject.com/ticket/32679>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 23, 2021, 12:33:23 PM4/23/21
to django-...@googlegroups.com
#32679: Django model not updating image field correctly (Raw content)
-------------------------------------+-------------------------------------
Reporter: mcardozo | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: models | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 1
-------------------------------------+-------------------------------------
Description changed by mcardozo:

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>

Django

unread,
Apr 23, 2021, 1:33:28 PM4/23/21
to django-...@googlegroups.com
#32679: Django model not updating image field correctly (Raw content)
-------------------------------------+-------------------------------------
Reporter: Marisol Cardozo | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution: invalid

Keywords: models | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* 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>

Reply all
Reply to author
Forward
0 new messages