Creating an image slider in Django using JSON
JSON is a great choice for storing and retrieving data which does not require querying and filtering. In such cases JSON might offer better performance than separate columns and foreignkeys.
One such use case is image sliders.
The traditional method to implement an image slider in Django is to create two
models—Slider
and Slide
. The Slide
model will have a ForeignKey
to the
Slider
model so that one slider can have many slides.
However, our use case will never require querying individual slides from the database.
We'll always select a Slider
object from the database and then loop over its
related slides.
Thus, We can ditch the foreignkey and keep the slides in a JSON array in the same
Slider
model.
This will give us slightly better performance.
Demo app¶
Github repo: http://github.com/bhch/json-slider-demo.
Download the demo app, install it and test it out.
The repo contains usage instructions. You can modify it as per your needs.
The demo app utilizes django-jsonform library to create a nice, user-friendly form for editing the JSON data.
Final result¶
A short video of the final result: