Building RESTful Python Web Services Chapter 3
Chapter 3: Improving and Adding Authentication to an API With Django
Adding unique constraints to the models
The author introduces theunique=True keyword/phrase. This allows Django to add the unique constraint to the database table. He also goes over how to run migrations one more time. We then test the code, and voila it just works. I was personally glad for the rerun of the makemigrations and migrate commands. I've been using it for a while now, so it wasn't real helpful, but I can remember in the beginning, having to wrack my brain for the correct commands to run to alter the database.Updating a single field for a resource with the PATCH method
This is a very quick section on, you guessed it, the PATCH method. Very simple, and straightforward to use. I am still a little confused on how all this works. I think the author would have done a better job explaining the secret sauce in the rest_framework. Some self study is required at this point in the book.Taking advantage of pagination
This is a really great little section on the power of Django, and the rest_framework. By adding a few lines of code in your settings, you can start with an arbitrary limit of options to return. And by adding a few lines to your JSON input, you can mix and match results to get the whole listing from the database, thereby making your life much easier if you need to use the API in some other place.Customizing pagination classes
The author uses this section to override the pagination class with an upper limit on the number of results that can be returned. While he is using the pagination class as an example, this shows how easy it is to override a class in not only Django, but also Python.One caveat, make sure you pay attention to the changes in the settings.py file. I didn't and I kept getting an import error. A simple mistake to make. I think the author should have highlighted the changes as he has done in previous sections. That would have made it much easier to keep track of. Again, I highly recommend typing the changes, and not just copying and pasting them from the downloaded code.
Comments
Post a Comment