本文整理汇总了Python中django.db.models.fields.NOT_PROVIDED属性的典型用法代码示例。如果您正苦于以下问题:Python fields.NOT_PROVIDED属性的具体用法?Python fields.NOT_PROVIDED怎么用?Python fields.NOT_PROVIDED使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类django.db.models.fields
的用法示例。
在下文中一共展示了fields.NOT_PROVIDED属性的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: conv_NullBooleanField
# 需要导入模块: from django.db.models import fields [as 别名]
# 或者: from django.db.models.fields import NOT_PROVIDED [as 别名]
def conv_NullBooleanField(self, model, field, kwargs):
from django.db.models.fields import NOT_PROVIDED
def coerce_nullbool(value):
d = {'None': None, None: None, 'True': True, 'False': False}
if isinstance(value, NOT_PROVIDED):
return None
elif value in d:
return d[value]
else:
return bool(int(value))
choices = ((None, 'Unknown'), (True, 'Yes'), (False, 'No'))
return f.SelectField(choices=choices, coerce=coerce_nullbool, **kwargs)
示例2: set_owner
# 需要导入模块: from django.db.models import fields [as 别名]
# 或者: from django.db.models.fields import NOT_PROVIDED [as 别名]
def set_owner(self):
if hasattr(self, "OWNER_CLASS_NAME"):
owner_class = self.get_model_class_by_name(self.OWNER_CLASS_NAME)
if not owner_class:
raise XOSValidationError(
"Cannot find owner class %s" % self.OWNER_CLASS_NAME
)
need_set_owner = True
if self.owner_id:
# Check to see if owner is set to a valid instance of owner_class. If it is, then we already have an
# owner. If it is not, then some other misbehaving class must have altered the ServiceInstance.meta
# to point to its own default (these services are being cleaned up).
if owner_class.objects.filter(id=self.owner_id).exists():
need_set_owner = False
if need_set_owner:
owners = owner_class.objects.all()
if not owners:
raise XOSValidationError(
"Cannot find eligible owner of class %s" % self.OWNER_CLASS_NAME
)
self.owner = owners[0]
else:
# Deal with legacy services that specify their owner as _meta field default. This is a workaround for
# what is probably a django bug (if a SerivceInstance without a default is created before a ServiceInstance
# that does have a default, then the later service's default is not honored by django).
# TODO: Delete this after all services have been migrated away from using field defaults
if (
(not self.owner_id)
and (self._meta.get_field("owner").default)
and (self._meta.get_field("owner").default != NOT_PROVIDED)
):
self.owner = Service.objects.get(
id=self._meta.get_field("owner").default
)
示例3: conv_NullBooleanField
# 需要导入模块: from django.db.models import fields [as 别名]
# 或者: from django.db.models.fields import NOT_PROVIDED [as 别名]
def conv_NullBooleanField(self, model, field, kwargs):
from django.db.models.fields import NOT_PROVIDED
def coerce_nullbool(value):
d = {'None': None, None: None, 'True': True, 'False': False}
if isinstance(value, NOT_PROVIDED):
return None
elif value in d:
return d[value]
else:
return bool(int(value))
choices = ((None, 'Unknown'), (True, 'Yes'), (False, 'No'))
return f.SelectField(choices=choices, coerce=coerce_nullbool, **kwargs)
示例4: field_sort_key
# 需要导入模块: from django.db.models import fields [as 别名]
# 或者: from django.db.models.fields import NOT_PROVIDED [as 别名]
def field_sort_key(field):
return (
dec(field.primary_key),
dec(field.unique),
inc(field.null),
inc(field.blank),
dec(field.default is NOT_PROVIDED),
inc(field.name),
)
示例5: test_add_field_preserve_default
# 需要导入模块: from django.db.models import fields [as 别名]
# 或者: from django.db.models.fields import NOT_PROVIDED [as 别名]
def test_add_field_preserve_default(self):
"""
Tests the AddField operation's state alteration
when preserve_default = False.
"""
project_state = self.set_up_test_model("test_adflpd")
# Test the state alteration
operation = migrations.AddField(
"Pony",
"height",
models.FloatField(null=True, default=4),
preserve_default=False,
)
new_state = project_state.clone()
operation.state_forwards("test_adflpd", new_state)
self.assertEqual(len(new_state.models["test_adflpd", "pony"].fields), 4)
field = [
f for n, f in new_state.models["test_adflpd", "pony"].fields
if n == "height"
][0]
self.assertEqual(field.default, NOT_PROVIDED)
# Test the database alteration
project_state.apps.get_model("test_adflpd", "pony").objects.create(
weight=4,
)
self.assertColumnNotExists("test_adflpd_pony", "height")
with connection.schema_editor() as editor:
operation.database_forwards("test_adflpd", editor, project_state, new_state)
self.assertColumnExists("test_adflpd_pony", "height")
# And deconstruction
definition = operation.deconstruct()
self.assertEqual(definition[0], "AddField")
self.assertEqual(definition[1], [])
self.assertEqual(sorted(definition[2]), ["field", "model_name", "name", "preserve_default"])
示例6: _get_default_value
# 需要导入模块: from django.db.models import fields [as 别名]
# 或者: from django.db.models.fields import NOT_PROVIDED [as 别名]
def _get_default_value(serializer, field_name, field):
"""
Try to get default value for a field and format it nicely.
"""
value = field.default
if hasattr(value, 'doc_format'):
return _get_type_from_docstring(value.doc_format)
if value == fields.empty:
# Try to get default from model field.
try:
default = serializer.Meta.model._meta.get_field(field_name).default
return default if default != NOT_PROVIDED else None
except (FieldDoesNotExist, AttributeError):
return None
return value
开发者ID:product-definition-center,项目名称:product-definition-center,代码行数:17,代码来源:renderers_serializers.py
示例7: __init__
# 需要导入模块: from django.db.models import fields [as 别名]
# 或者: from django.db.models.fields import NOT_PROVIDED [as 别名]
def __init__(self, *args, **kwargs):
super(NullToDefaultMixin, self).__init__(*args, **kwargs)
for field in self.Meta.fields:
try:
model_field = self.Meta.model._meta.get_field(field)
if hasattr(model_field, 'default') and model_field.default != NOT_PROVIDED:
self.fields[field].allow_null = True
except FieldDoesNotExist:
pass
示例8: validate
# 需要导入模块: from django.db.models import fields [as 别名]
# 或者: from django.db.models.fields import NOT_PROVIDED [as 别名]
def validate(self, data):
for field in self.Meta.fields:
try:
model_field = self.Meta.model._meta.get_field(field)
if hasattr(model_field, 'default') and model_field.default != NOT_PROVIDED and \
data.get(field, NOT_PROVIDED) is None:
data.pop(field)
except FieldDoesNotExist:
pass
return super(NullToDefaultMixin, self).validate(data)
示例9: update_default_from_model
# 需要导入模块: from django.db.models import fields [as 别名]
# 或者: from django.db.models.fields import NOT_PROVIDED [as 别名]
def update_default_from_model(self, rv, model_field):
if model_field is None:
return
if hasattr(model_field, 'default') and model_field.default != NOT_PROVIDED:
rv['default'] = model_field.default
示例10: build_standard_field
# 需要导入模块: from django.db.models import fields [as 别名]
# 或者: from django.db.models.fields import NOT_PROVIDED [as 别名]
def build_standard_field(self, field_name, model_field):
field_class, field_kwargs = super(SampleSerializer, self).build_standard_field(field_name, model_field)
if model_field.default is not NOT_PROVIDED:
field_kwargs['default'] = model_field.default
return field_class, field_kwargs
示例11: bulk_insert
# 需要导入模块: from django.db.models import fields [as 别名]
# 或者: from django.db.models.fields import NOT_PROVIDED [as 别名]
def bulk_insert(object_list, show_sql = False):
"""
Generate the sql code for bulk insertion
@param object_list: Django model objects
"""
if not len(object_list):
return
Model = type(object_list[0])
table_name = Model._meta.db_table
fields_names = [ f.attname for f in Model._meta.fields if f.name != "id" ]
sql = "insert into " + table_name + ' (' + ','.join(fields_names) + ') values \n'
defaults = dict([(f.attname, f.default if f.default is not NOT_PROVIDED else "NULL") for f in Model._meta.fields])
auto_now_add = [f.attname for f in Model._meta.fields if getattr(f, "auto_now_add", False)]
def get_values(ob, fields):
ret = []
for field in fields:
val = getattr(ob, field)
if val is None:
val = defaults[field]
if field in auto_now_add:
val = date.today().strftime("%Y-%m-%d")
ret.append(str(val))
return ret
lines = []
for ob in object_list:
line = '("' + '","'.join(get_values(ob, fields_names)) + '")'
line = line.replace('"NULL"', 'NULL')
line = line.replace('"False"', 'False')
line = line.replace('"True"', 'False')
lines.append(line)
sql += ',\n'.join(lines) + ";"
# genesfile = open('sqlgenes', 'w')
if show_sql:
print(sql)
return
cursor = connection.cursor()
# genesfile.writelines(sql)
cursor.execute(sql)
transaction.commit_unless_managed()