Opened 5 years ago
Closed 5 years ago
#32679 closed Bug (invalid)
Django model not updating image field correctly (Raw content)
| Reported by: | Marisol Cardozo | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 3.1 |
| Severity: | Normal | Keywords: | models |
| Cc: | cardozomarisolp@… | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
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.
Change History (2)
comment:1 by , 5 years ago
| Description: | modified (diff) |
|---|
comment:2 by , 5 years ago
| Resolution: | → invalid |
|---|---|
| Status: | new → closed |
| UI/UX: | unset |
Note:
See TracTickets
for help on using tickets.
You cannot use
.update()for updating aFileField/ImageField, in that way you will update only a filename in the database. You should use 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.