Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#265): async image upload example - fully working #18

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

markdoerr
Copy link
Collaborator

Hi @AMontagu and @legau,

after a long search, I finally managed to build a working example of an Image Upload and Update.

The sync_to_async part still looks a little bit clumsy - is there a nicer way to formulate it ?

If not, please merge it into master and correct the documentation.

Merci.

response_name="UploadStatus",
request_stream=True,
)
async def UpdateImage(self, request, context):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UploadImage may be a better name

request_name="FileChunk",
response=[{"name": "success", "type": "bool"}],
response_name="UploadStatus",
request_stream=True,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not using a response stream too to send upload progress ?


queryset = self.get_queryset()

book = await sync_to_async(queryset.get)(book_id=book_id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

book = await queryset.aget(book_id=book_id)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually if you use the lookupfield in the request (remplacing book_id by just id) you can directly do something like:

book = await self.aget_object()

book = await sync_to_async(queryset.get)(book_id=book_id)

print("----- found:", book.title)
res = await sync_to_async(book.image.save)(filename, f, save=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

res = await book.image.asave(filename, f, save=True)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants