当我hstore.DictionaryField()
在Django模型中使用时,不传递任何参数并在Djano admin中注册我的模型,我可以在管理界面中快速创建新的键值行。
当我在模式模式下使用相同的字段时,hstore.DictionaryField(schema=['some schema description'])
我得到了schema
参数中描述的固定数量的字段。
我是否可以同时具有这两个功能,即在架构描述中列出了某些特定类型的固定字段,同时还可以添加新字段?
更新
解决此问题的一种方法可能是使用两个DictionaryField的一个具有模式而另一个是无模式的,但这并不是最佳解决方案。
答案是否定的,您不能同时使用hstore库的当前实现。看一下init函数(这是从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 UI中编辑该值。
不幸的是,您只有几个选择:自己创建一个继承自该类的类来覆盖它,或者在他们的github页面上打开拉取请求或问题,希望有人能得到它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句