๐ Search, Uploads, & Automatic Deploys ๐
Posted on Jul 26th, 2021
Todayโs Topics
- Postgres full-text search
- Automatic deploys with Herokuโs GitHub integration
- File upload and Amazon S3
๐ฏ Project: QuestionBox is due on Thursday
By today you should have working endpoints for:
- login and logout
- registration
- all questions
- all questions for a single user (authenticated user; own questions only)
- all answers for a single user (authenticated user; own answers only)
- create a question (authenticated users only)
- details for a single question
- all answers for a single question
- create an answer for a question (authenticated users only)
Depending on how youโve constructed your API, you might have separate endpoints for all of the above, or you might have fewer endpoints (for instance, if you nested answers in the question detail endpoint). What matters is that your have endpoints your front-end team can use to access this data.
This week you should work on endpoints for:
- search all questions
- optionally search questions and answers
- delete a question (for user who created the question)
- mark an answer as accepted (only if you are the author of the associated question)
- favorite/โstarโ a question (authenticated users only)
๐ Read | ๐บ Watch | ๐ง Listen
- ๐ File Upload with DRF
- ๐ง + ๐ Success with Static Files
- ๐ Basic and Full-Text Search with Django and Postgres
- ๐ Blog post with more on full-text search
- Search from the Ground Up -> DjangoCon 2019 video explaining search in detail
- ๐ What is Amazon S3? -> Skim this โ this is Amazonโs documentation and it gets really in-depth.
- ๐บ Introduction to S3 -> Also from Amazon
๐ Resources
@action
decorator in ViewSets
- DRF Docs: Marking extra actions for routing with the
@action
decorator - DRF Docs: Routing for extra actions
Filtering and search
- DRF - Filtering -> Pretty useful reference. Includes how to filter your output based on GET parameters, which you will want to do for using search terms.
- Django Docs: full-text search & the search lookup
- Django Docs: SearchVector -> Youโll need this if you want to search against more than a single field
File uploads
- Django Docs: ImageField
- Django Docs: FileField
- Pillow: Python Imaging Library
django-imagekit
-> If you want to resize images when they are uploaded, or do any kind of image processing, you will need this. Donโt add it unless you know you need it.
- How to Setup Amazon S3
django-storages
- DRF
FileUploadParser
POST with upload using Insomnia
- choose binary file attachment
-
headers (this example assumes an image file in jpeg format, named
profile-photo .jpg
):Content-Type: image/jpeg Content-Disposition: attachment; filename=profile-photo.jpg
For more information on the values for Content-Type
:
CORS for file upload
Assuming you are using django-cors-headers
, youโll need to add the following to settings.py
:
from corsheaders.defaults import default_headers
CORS_ALLOW_HEADERS = list(default_headers) + [
'content-disposition',
]