I’ve been developing product catalog. All my needs fit perfectly with MongoDB but there is one problem:
There are categories with structure like these:
{
title:"Auto", // for h1 on page
id: 1, // for urls
level: 1, //
left: 2, // nested set implementation
right:10 //
}
When user goes to url like “http://www.example.com/category/1” I need to show html forms to filter shown goods. I’m pretty sure it is bad idea to store static definitions for every category on the server(about 300 categories, i’m using Django => 300 form models?)
So my idea is to store meta-information about each category in MongoDB. After some research I found out that I need 3-4 “html widgets” to construct any filter form that I need.
For example:
Range fields will look like this in HTML
<fieldset>
<legend>Price</legend>
<label for="price_from">from</label>
<input type="text" name="price_from">
<label for="price_to">to</label>
<input type="text" name="price_to">
</fieldset>
And its MongoDB JSON representation:
{
name:"price",
title:"Price",
type:"range", // "range" type - 2 inputs with initial values
categories:[1,2,5],// categories to which this so called "widget" relates
values:[
{from: 100},
{to: 500}
]
}
One more example:
Group of selects:
Its HTML version
<fieldset>
<legend>Size and color</legend>
<select name="size">
<option value="small">Small size</option>
<option value="medium">Medium size</option>
<option value="huge">Huge size</option>
</select>
<select name="color">
<option value="white">White color</option>
<option value="black">Black color</option>
<option value="purple">Purple color</option>
</select>
</fieldset>
And its MongoDB JSON version:
{
title:"Size and color",
type:"selectgroup",
categories:[2,4,6]
items:[
{
name:"size",
values:["small", "medium", "huge"]
},
{
name:"color",
values:["white", "black", "purple"]
}
]
}
So the main idea is : fetch all widgets from collection by category_id, parse them and print complete HTML form.
Pros
- Easy to add any new type of widget to database
- Easy to add parser for such widget to generate HTML from JSON
- Each widget can relates to many categories => no duplicates
Cons
- Collection of widgets has no less or more fixed structure of the document(actually i don’t see real problem here)
- Аll values related to widget are embedded.
- Hard validation because fields are dynamic => no corresponding model on server side (right now i don’t have any idea how to validate this)
My question is: does anybody know a better way of doing this? Pros of my method are cool, but cons are terrible.
Thanks for help and sorry for bad English.
If you only need 3-4 sets of form widgets to display all necessary query forms for the 300+ categories, you should use Django forms to specify the widgets. You do not need 300+ Django forms for that as you can easily use multiple forms in one page.
As a quick example, your
would become
…and of course the template to embed the form into which would provide the
<fieldset>and other necessary html document structure.EDIT:
As it seems that your form labels etc. depend on the category, you still need to have some information about the forms in the category documents in mongodb. However, by using django forms you get a lot of useful functionality for free, such as input validation.
You probably can parametrize the templates in such a way that you give the category-specific values (labels etc.) to the template from mongodb, along with the necessary forms.