在Django 1.9中,使用JSONField(native postgres jsonb)的约定是什么? [英] In Django 1.9, what's the convention for using JSONField (native postgres jsonb)?
问题描述
Django 高度建议 不使用 null = True
for CharField和TextField基于字符串的字段,以便不具有no data的两个可能值(假设您允许空字符串为空白=真
)。这对我来说非常有意义,我在所有的项目中都这样做。
Django highly suggests not to use null=True
for CharField and TextField string-based fields in order not to have two possible values for "no data" (assuming you're allowing empty strings with blank=True
). This makes total sense to me and I do this in all my projects.
Django 1.9引入了 JSONField ,它使用底层的Postgres jsonb
数据类型。上述建议是否应使用JSONField(即 blank = True
而不是 null = True
)?或者,应该使用 null = True
?或者,应该使用 default = dict
?要么, ..?为什么?
Django 1.9 introduces JSONField, which uses the underlying Postgres jsonb
data type. Does the suggestion above carry over to JSONField (i.e. blank=True
should be used instead of null=True
)? Or, should null=True
be used instead? Or, should default=dict
be used instead? Or, ..? Why?
换句话说,当您只允许一个无数据值时,新的本机JSONField的约定是什么?请支持你的答案,因为我做了大量的研究,找不到任何官方的。感谢提前。
In other words, what is the convention for the new native JSONField, when you want to allow only one "no data" value? Please support your answer because I did a lot of research and couldn't find anything official. Thanks in advance.
推荐答案
Django代码暗示的约定似乎是将而不是一个空字符串(如 CharField
的惯例)。我这样说是因为以下原因:
The convention implied from the Django code seems to be to store null JSON values as NULL as opposed to as an empty string (as is the convention for the CharField
). I say this because of the following:
empty_strings_allowed
继承自 Field
CharField ,并设置为 True
:
The empty_strings_allowed
is inherited from Field
in CharField
, and is set to True
:
django / db /模型/字段/ __ init __。py#L96
class Field(RegisterLookupMixin):
"""Base class for all field types"""
# Designates whether empty strings fundamentally are allowed at the
# database level.
empty_strings_allowed = True
...
JSONField
,但是用 False
覆盖:
django / contrib / postgres / fields / jsonb.py#L13
class JSONField(Field):
empty_strings_allowed = False
...
这导致 CharField
的默认值为和
JSONField
到无
当您实例化模型而不显式传递这些字段的值。
This causes CharField
's to default to ""
and JSONField
's to None
when you instantiate a model without explicitly passing the values for these fields.
django / db / models / fields / init .py#L791
django/db/models/fields/init.py#L791
def get_default(self):
"""
Returns the default value for this field.
"""
if self.has_default():
if callable(self.default):
return self.default()
return self.default
if (not self.empty_strings_allowed or (self.null and
not connection.features.interprets_empty_strings_as_nulls)):
return None
return ""
因此,如果要制作一个 JSONField
可选,则必须使用:
Therefore, if you want to make a JSONField
optional, you have to use:
json_field = JSONField(blank=True, null=True)
如果您只使用 blank = True
,就像 CharField
一样,尝试运行 MyModel.objects.create(...)
而不传递<$ c时,会得到一个 IntegrityError
$ c> json_field 参数显式。
If you use only blank=True
, as you would for CharField
, you'll get an IntegrityError
when trying to run MyModel.objects.create(...)
without passing a json_field
argument explicitly.
这篇关于在Django 1.9中,使用JSONField(native postgres jsonb)的约定是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!