2016-07-07 64 views
2

當我在Django模型中使用hstore.DictionaryField()傳遞無參數並在Djano admin中註冊我的模型時,我可以在管理界面中快速創建新的鍵值行。如何在模式和無模式模式下同時使用Django HStore DictionaryField?

當我在模式模式hstore.DictionaryField(schema=['some schema description'])中使用相同的字段時,我獲得了固定數量的schema參數中描述的字段。

我可以同時擁有這兩個功能嗎?即,有幾個固定字段可以在模式描述中列出某種類型,同時還可以添加新字段?

UPD

一個解決這種方式可以使用 DictionaryField的一個與架構等是無模式的,但它是很難的最佳解決方案。

回答

0

答案是否定的,您不能同時使用hstore庫的當前實現。看看初始化函數(這是從他們在github源):

class DictionaryField(HStoreField): 
    description = _("A python dictionary in a postgresql hstore field.") 

    def __init__(self, *args, **kwargs): 
     self.schema = kwargs.pop('schema', None) 
     self.schema_mode = False 
     # if schema parameter is supplied the behaviour is slightly different 
     if self.schema is not None: 
      self._validate_schema(self.schema) 
      self.schema_mode = True 
      # DictionaryField with schema is not editable via admin 
      kwargs['editable'] = False 
      # null defaults to True to facilitate migrations 
      kwargs['null'] = kwargs.get('null', True) 

     super(DictionaryField, self).__init__(*args, **kwargs) 

你可以看到,如果該模式被設置,則該字段的編輯參數設置爲false,這是什麼Django admin會查看是否允許您在管理界面中編輯值。

不幸的是,你只有幾個選擇:通過創建自己的類來繼承它,或者在他們的github頁面上打開拉請求或問題,並希望有人能夠接受它。

+0

hstore已經看起來太強大了。感謝您的通知。 –