1

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 "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.

4

2 に答える 2

1

300 以上のカテゴリに必要なすべてのクエリ フォームを表示するために 3 ~ 4 セットのフォーム ウィジェットしか必要ない場合は、Django フォームを使用してウィジェットを指定する必要があります。1 ページで複数のフォームを簡単に使用できるため、300 以上の Django フォームは必要ありません。

簡単な例として、あなたの

<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>

になるだろう

class PriceForm(forms.Form):
    price_from = forms.IntegerField()
    price_to = forms.IntegerField()

...そしてもちろん、<fieldset>その他の必要な html ドキュメント構造を提供するフォームを埋め込むためのテンプレート。

編集: フォーム ラベルなどはカテゴリに依存しているように見えるため、mongodb のカテゴリ ドキュメント内のフォームに関する情報が必要です。ただし、django フォームを使用すると、入力の検証など、多くの便利な機能を無料で利用できます。

おそらく、必要なフォームとともに、mongodb からテンプレートにカテゴリ固有の値 (ラベルなど) を与えるような方法で、テンプレートをパラメーター化できます。

于 2012-05-06T19:19:43.247 に答える
1

Map/Reduce を使用して動的にフィルター処理された (ファセット) 検索を構築する手法: http://ianwarshak.posterous.com/faceted-search-with-mongodb

コードを Python に正常に移行しました (そして Django プロジェクトで使用しました)。

于 2012-05-07T10:37:28.393 に答える