I am new to django. I’m trying create an ecommerce application in which the user will select from a category of options [automobile, property, electronics, phones, clothes etc], and upon selecting an option (say automobile), the next select field options will be populated asking for model [Honda, Toyota, Chrysler etc], if user select say Toyota, a list of Toyota models comes up [Camry, Highlander, Corolla, etc.], upon selecting say Camry, another set of options comes up asking for year, and so on.
How do I implement (generate and/or store) this type of data using django models? I’ll be very grateful for a swift response to this.
Thank you.
It’s not the actual answer to your question, but when I read it, the htmx Cascading Selects example came into my mind, which could help you to build the frontend.
When it comes to the models, I would probably try one for the items and another one for the categories which should be nested somehow. I am using django-treenode (for a different purpose), perhaps this could be worth a look.
To augment the prior answer, anything of this nature requires some JavaScript in the browser to do it. Your model design alone isn’t enough.
In the general sense, some JavaScript (direct or using the framework of your choice) needs to be triggered by the on-change event of the first drop-down. That JavaScript would issue an AXAJ GET to a custom view in Django to retrieve the next widget’s data (or an HTML fragment for the options). The view returns the data back to the JavaScript, which then injects the data into the page.
There are some third-party packages that can be plugged into your application, but you probably want to find one that matches whatever JavaScript framework you’re currently using.
Also see: Django: how to choose from a list based on the previous selection? for one such implementation.