Posting Django FileFields before they're saved
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 434
- Forks
- 103
- PR merge metrics
- No merged PRs in 30d
Description
I'm having issues when trying to POST Django FileFields files before they're saved. I'm trying to post them during a model save override (to get extra data). So in MyModel.save override it looks like this:
class MyModel(models.Model):
image = models.ImageField()
def save(self, *args, **kwargs):
res = unirest.post(url, headers=headers, params={
"image_request[image]": self.image.file
})
# @todo: add some field from res.body to my model.
super(MyModel, self).save(*args, **kwargs)
Problem is, before the model is saved, self.image.file is either an instance of InMemoryUploadedFile or TemporaryUploadedFile. When I have the latter (or after model is saved), I have to pass in open(self.image.file.path), as self.image.file.open() won't work. I wonder why.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by comparing Django's InMemoryUploadedFile and TemporaryUploadedFile behavior in django/core/files/uploadedfile.py with unirest's file-upload request handling. Determine whether the client documents or supports these file-like objects, and define completion as a clear explanation or a reproducible fix for posting unsaved FileFields.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- django, python
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100