How to store arbitrary name/value key pairs in a Django model?

后端 未结 5 523
没有蜡笔的小新
没有蜡笔的小新 2021-02-03 11:02

I have a fixed data model that has a lot of data fields.

class Widget(Models.model):
    widget_owner = models.ForeignKey(auth.User)
    val1 = models.CharField()
            


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-03 11:11

    In my opinion, the best way to achieve this sort of completely extensible model is really with EAV (Entity, Attribute, Value). Its basically a way to bring a schemaless non-relational database to SQL. You can read a bunch more about it on wikipedia, http://en.wikipedia.org/wiki/Entity-attribute-value_model but one of the better implementation of it in django is from the EveryBlock codebase. Hope it's a help!

    http://github.com/brosner/everyblock_code/blob/master/ebpub/ebpub/db/models.py

提交回复
热议问题