2009-08-07 12 views
1

私はDjangoでユーザープロファイルアプリケーションを作成したいと思います(ありがとうございます。ありがとうございます)、モデルを構造化して各サブセクションの任意の組み合わせフィールド。Django:任意のフィールドタイプを許可する構造Djangoモデル

例として、「教育」セクションには「プログラミングエクスペリエンス」というサブセクションがあり、「個人情報」セクションには「お気に入り」というサブセクションがあります。

典型的なサイドバーのナビゲーション設定では、各セクションはヘッダーになり、各サブセクションは情報を操作できるフォームへのリンクになります。

Education 
- Schooling 
- Programming Experience 

Personal Info 
- Location 
- Favourites 
- Look a-likes 

私がしたいのは、任意の基準でサブセクションに項目を追加できるようにすることです。サイトの雰囲気がどんなものかを問わず。

ユーザーが参加した学校の写真が1つのサイトに役立つ場合があり、別のサイトには説明が必要な場合があります。

adminインターフェイスを使用して、これらのフィールドタイプをサブセクションに追加したいと考えています。したがって、アイテムを追加すると、それがどのタイプの情報(画像、ビデオ、テキストなど)とそれに適用されるべきサブセクションの選択肢が提示されます。

これをどのように達成するのかを知りたいです。より重要なのは、可能な限り少ないフープでジャンプすることです。

ありがとうございました。

編集:

うまくいけば、私はサンプルmodels.pyファイルを提供します質問を明確にします。これは、問題をより正確に示すための簡単なモチーフです。私は2つのソリューションを念頭に置いています。ソリューション2はソリューション1よりもうまくいくと思います。私はSOコミュニティーが何を考えているのか、それに他の解決策があるのか​​をここにも示したいと思います。

**models.py** 

class Section(models.Model): 
    """ 
    The root of categorization. Acts much like a header 
    """ 
    name = models.CharField(max_length=30) 
    description = models.CharField(max_length=255) 

class SubSection(models.Model): 
    """ 
    The contents of each section. Contains many items of varying types as needed 
    by the site developer. 
    """ 
    name = models.CharField(max_length=30) 
    description = models.CharField(max_length=255) 
    section = models.ForeignKey(Section) 

class Item(models.Model): 
    """ 
    I would like this to store the information here and have a foreign key to the 
    'SubSection' table. The problem is that there are a lot of different information 
    types that can be stored and I'd need a row for each type. Thus for each 
    entry most of the columns will be blank. 

    I'm thinking that it may be better to use this table as a pointer to another 
    table that actually contains the information. This will result in a lot of 
    tables but will eliminate waste. 
    """ 

    name = models.CharField(max_length=30) 
    description = models.CharField(max_length=255) 
    sub_section = models.ForeignKey(SubSection) 

    ### Solution One 
    # Storing the info here results in a lot of wasted space and may not be all 
    # that flexible 
    image = models.ImageField() 
    text = models.CharField(max_length=255) 
    numeric = models.IntegerField() 
    time = models.TimeField() 
    # etc ... 

    ### Solution Two 
    # Storing the field info results in more tables but allows for a better match 
    # of information and field type. 
    field_type = models.CharField(max_length=255) 
    field_properties = models.CommaSeparatedIntegerField(max_length=None) 


### Solution Two Tables 
# Solution two would require a table for each field type supported here, which 
# is quite a few different types. 

class ImageStorage(models.Model): 
    item = models.ForeignKey(Item) 
    information = models.ImageField() 

class IntegerStorage(models.Model): 
    item = models.ForeignKey(Item) 
    information = models.IntegerField() 

### etc ... 

ユーザープロファイルをターゲットにしています。だから減量サイトは、旅行サイトが訪れた場所のリスト(テキスト情報は、私が想定しているIPAddressFieldを使用することもできます)を必要とするかもしれない一方、プロファイル(数値情報)のユーザーの現在の重量を望むかもしれません。何がポップアップするかわからないので、できるだけジェネリックにしようとしています。

答えて

0

私があなたを正しく理解しているなら、これを行う最も簡単な方法は多対1の関係になるでしょう。ユーザー単位またはサイト単位でこれらを使用するかどうかはわかりませんので、サイト単位でカスタマイズしたいと思っています(簡単に切り替えることができます)。

次のようなものになりますテーブルを作成します。あなたが使用して添付のサブセクションのためのDBを照会できるように、同じセクションに属している複数のサブセクションについて

class Section(models.Model): 
    section = models.CharField() 
    sub-section = model.CharField() 
    site = models.ForeignKey(Site) 

を、単にそれらを同じプライマリセクション名を与えますその名前。

もう1つの方法は、同じことを達成するために2つのテーブルを使用することですが、アプリケーションを考えれば、これはより適切かもしれないと思います。

関連する問題