本文整理汇总了Python中django.db.models.NOT_PROVIDED属性的典型用法代码示例。如果您正苦于以下问题:Python models.NOT_PROVIDED属性的具体用法?Python models.NOT_PROVIDED怎么用?Python models.NOT_PROVIDED使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类django.db.models
的用法示例。
在下文中一共展示了models.NOT_PROVIDED属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compare_fields
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import NOT_PROVIDED [as 别名]
def compare_fields(instance, new_data, ignore_fields):
"""Compare a model instance to a new dict; return True if they match"""
for field in instance._meta.fields:
if field.name in ignore_fields:
# ignored fields
continue
current_value = getattr(instance, field.name)
if field.name not in new_data:
# no value provided, check if the current value is the default
default = field.default if field.default is not NOT_PROVIDED else None
if current_value == default:
continue
else:
return False
if current_value != new_data[field.name]:
return False
return True
示例2: add_field
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import NOT_PROVIDED [as 别名]
def add_field(self, model, field):
super(DatabaseSchemaEditor, self).add_field(model, field)
# Simulate the effect of a one-off default.
if (self.skip_default(field)
and field.default not in (None, NOT_PROVIDED)):
effective_default = self.effective_default(field)
self.execute('UPDATE %(table)s SET %(column)s = %%s' % {
'table': self.quote_name(model._meta.db_table),
'column': self.quote_name(field.column),
}, [effective_default])
示例3: add_field
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import NOT_PROVIDED [as 别名]
def add_field(self, model, field):
super(DatabaseSchemaEditor, self).add_field(model, field)
# Simulate the effect of a one-off default.
if self.skip_default(field) and field.default not in {None, NOT_PROVIDED}:
effective_default = self.effective_default(field)
self.execute('UPDATE %(table)s SET %(column)s = %%s' % {
'table': self.quote_name(model._meta.db_table),
'column': self.quote_name(field.column),
}, [effective_default])
示例4: database_forwards
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import NOT_PROVIDED [as 别名]
def database_forwards(self, app_label, schema_editor: BaseDatabaseSchemaEditor, from_state, to_state: ProjectState):
to_model = to_state.apps.get_model(app_label, self.model_name)
meta = to_model._meta
to_field = meta.get_field(self.name)
if to_field.default != NOT_PROVIDED:
table_name = schema_editor.quote_name(meta.db_table)
column = schema_editor.quote_name(to_field.column)
default = schema_editor.quote_value(to_field.default)
schema_editor.execute("ALTER TABLE {} ALTER COLUMN {} SET DEFAULT {}".format(table_name, column, default))
示例5: add_field
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import NOT_PROVIDED [as 别名]
def add_field(self, model, field):
super().add_field(model, field)
# Simulate the effect of a one-off default.
# field.default may be unhashable, so a set isn't used for "in" check.
if self.skip_default(field) and field.default not in (None, NOT_PROVIDED):
effective_default = self.effective_default(field)
self.execute('UPDATE %(table)s SET %(column)s = %%s' % {
'table': self.quote_name(model._meta.db_table),
'column': self.quote_name(field.column),
}, [effective_default])
示例6: add_field
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import NOT_PROVIDED [as 别名]
def add_field(self, model, field):
super(DatabaseSchemaEditor, self).add_field(model, field)
# Simulate the effect of a one-off default.
if (self.skip_default(field)
and field.default not in (None, NOT_PROVIDED)):
effective_default = self.effective_default(field)
self.execute('UPDATE %(table)s SET %(column)s = %%s' % {
'table': self.quote_name(model._meta.db_table),
'column': self.quote_name(field.column),
}, [effective_default])
示例7: get_field_value
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import NOT_PROVIDED [as 别名]
def get_field_value(obj, field):
"""
Gets the value of a given model instance field.
:param obj: The model instance.
:type obj: Model
:param field: The field you want to find the value of.
:type field: Any
:return: The value of the field as a string.
:rtype: str
"""
if isinstance(field, DateTimeField):
# DateTimeFields are timezone-aware, so we need to convert the field
# to its naive form before we can accuratly compare them for changes.
try:
value = field.to_python(getattr(obj, field.name, None))
if value is not None and settings.USE_TZ and not timezone.is_naive(value):
value = timezone.make_naive(value, timezone=timezone.utc)
except ObjectDoesNotExist:
value = field.default if field.default is not NOT_PROVIDED else None
else:
try:
value = smart_text(getattr(obj, field.name, None))
except ObjectDoesNotExist:
value = field.default if field.default is not NOT_PROVIDED else None
return value
示例8: add_field
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import NOT_PROVIDED [as 别名]
def add_field(self, model, field):
super(DatabaseSchemaEditor, self).add_field(model, field)
# Simulate the effect of a one-off default.
# field.default may be unhashable, so a set isn't used for "in" check.
if self.skip_default(field) and field.default not in (None, NOT_PROVIDED):
effective_default = self.effective_default(field)
self.execute('UPDATE %(table)s SET %(column)s = %%s' % {
'table': self.quote_name(model._meta.db_table),
'column': self.quote_name(field.column),
}, [effective_default])
示例9: test_alter_field_to_not_null_without_default
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import NOT_PROVIDED [as 别名]
def test_alter_field_to_not_null_without_default(self, mocked_ask_method):
"""
#23609 - Tests autodetection of nullable to non-nullable alterations.
"""
changes = self.get_changes([self.author_name_null], [self.author_name])
self.assertEqual(mocked_ask_method.call_count, 1)
# Right number/type of migrations?
self.assertNumberMigrations(changes, 'testapp', 1)
self.assertOperationTypes(changes, 'testapp', 0, ["AlterField"])
self.assertOperationAttributes(changes, "testapp", 0, 0, name="name", preserve_default=True)
self.assertOperationFieldAttributes(changes, "testapp", 0, 0, default=models.NOT_PROVIDED)
示例10: get_changes
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import NOT_PROVIDED [as 别名]
def get_changes(self, old_values, new_values):
""" Returns list of changed fields. """
diff = {}
old_values = old_values or {}
new_values = new_values or {}
fields = self.fields or [field_name.name for field_name in self.model_class._meta.fields]
for field_name in fields:
field = self.model_class._meta.get_field(field_name)
default = None
if field.default != NOT_PROVIDED:
default = field.default
old_value = old_values.get(field_name, default)
new_value = new_values.get(field_name, None)
old_value_string = ModelFieldStringifier.stringify(field, old_value)
new_value_string = ModelFieldStringifier.stringify(field, new_value)
if old_value is not None:
old_value = force_text(old_value)
if new_value is not None:
new_value = force_text(new_value)
if old_value != new_value:
diff[field_name] = {
'old_value': old_value,
'old_value_string': old_value_string,
'new_value': new_value,
'new_value_string': new_value_string
}
return diff
示例11: json_get_cached_first_disk_image
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import NOT_PROVIDED [as 别名]
def json_get_cached_first_disk_image(self):
if self._first_disk_image is NOT_PROVIDED:
self._first_disk_image = self.json_get_first_disk_image()
return self._first_disk_image
示例12: generate_altered_fields
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import NOT_PROVIDED [as 别名]
def generate_altered_fields(self):
"""
Fields that have been altered.
"""
for app_label, model_name, field_name in sorted(self.old_field_keys.intersection(self.new_field_keys)):
# Did the field change?
old_model_name = self.renamed_models.get((app_label, model_name), model_name)
old_field_name = self.renamed_fields.get((app_label, model_name, field_name), field_name)
old_field = self.old_apps.get_model(app_label, old_model_name)._meta.get_field(old_field_name)
new_field = self.new_apps.get_model(app_label, model_name)._meta.get_field(field_name)
# Implement any model renames on relations; these are handled by RenameModel
# so we need to exclude them from the comparison
if hasattr(new_field, "rel") and getattr(new_field.rel, "to", None):
rename_key = (
new_field.rel.to._meta.app_label,
new_field.rel.to._meta.model_name,
)
if rename_key in self.renamed_models:
new_field.rel.to = old_field.rel.to
old_field_dec = self.deep_deconstruct(old_field)
new_field_dec = self.deep_deconstruct(new_field)
if old_field_dec != new_field_dec:
both_m2m = (
isinstance(old_field, models.ManyToManyField) and
isinstance(new_field, models.ManyToManyField)
)
neither_m2m = (
not isinstance(old_field, models.ManyToManyField) and
not isinstance(new_field, models.ManyToManyField)
)
if both_m2m or neither_m2m:
# Either both fields are m2m or neither is
preserve_default = True
if (old_field.null and not new_field.null and not new_field.has_default() and
not isinstance(new_field, models.ManyToManyField)):
field = new_field.clone()
new_default = self.questioner.ask_not_null_alteration(field_name, model_name)
if new_default is not models.NOT_PROVIDED:
field.default = new_default
preserve_default = False
else:
field = new_field
self.add_operation(
app_label,
operations.AlterField(
model_name=model_name,
name=field_name,
field=field,
preserve_default=preserve_default,
)
)
else:
# We cannot alter between m2m and concrete fields
self._generate_removed_field(app_label, model_name, field_name)
self._generate_added_field(app_label, model_name, field_name)
示例13: generate_altered_fields
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import NOT_PROVIDED [as 别名]
def generate_altered_fields(self):
"""
Make AlterField operations, or possibly RemovedField/AddField if alter
isn's possible.
"""
for app_label, model_name, field_name in sorted(self.old_field_keys & self.new_field_keys):
# Did the field change?
old_model_name = self.renamed_models.get((app_label, model_name), model_name)
old_field_name = self.renamed_fields.get((app_label, model_name, field_name), field_name)
old_field = self.old_apps.get_model(app_label, old_model_name)._meta.get_field(old_field_name)
new_field = self.new_apps.get_model(app_label, model_name)._meta.get_field(field_name)
# Implement any model renames on relations; these are handled by RenameModel
# so we need to exclude them from the comparison
if hasattr(new_field, "remote_field") and getattr(new_field.remote_field, "model", None):
rename_key = (
new_field.remote_field.model._meta.app_label,
new_field.remote_field.model._meta.model_name,
)
if rename_key in self.renamed_models:
new_field.remote_field.model = old_field.remote_field.model
if hasattr(new_field, "remote_field") and getattr(new_field.remote_field, "through", None):
rename_key = (
new_field.remote_field.through._meta.app_label,
new_field.remote_field.through._meta.model_name,
)
if rename_key in self.renamed_models:
new_field.remote_field.through = old_field.remote_field.through
old_field_dec = self.deep_deconstruct(old_field)
new_field_dec = self.deep_deconstruct(new_field)
if old_field_dec != new_field_dec:
both_m2m = old_field.many_to_many and new_field.many_to_many
neither_m2m = not old_field.many_to_many and not new_field.many_to_many
if both_m2m or neither_m2m:
# Either both fields are m2m or neither is
preserve_default = True
if (old_field.null and not new_field.null and not new_field.has_default() and
not new_field.many_to_many):
field = new_field.clone()
new_default = self.questioner.ask_not_null_alteration(field_name, model_name)
if new_default is not models.NOT_PROVIDED:
field.default = new_default
preserve_default = False
else:
field = new_field
self.add_operation(
app_label,
operations.AlterField(
model_name=model_name,
name=field_name,
field=field,
preserve_default=preserve_default,
)
)
else:
# We cannot alter between m2m and concrete fields
self._generate_removed_field(app_label, model_name, field_name)
self._generate_added_field(app_label, model_name, field_name)
示例14: get_fields_data
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import NOT_PROVIDED [as 别名]
def get_fields_data(self):
fields = OrderedDict()
serializer_class = self.options_serializer or self.get_serializer_class()
try:
instance = self.get_object()
except:
instance = None
serializer = serializer_class(instance)
model = serializer.Meta.model
for field_code, field_class in serializer.get_fields().items():
field_type = field_class.__class__.__name__
fields[field_code] = {'field_type': field_type,
'ui_element': self.get_ui_element(field_code, field_class)}
for field_attr_name in self.default_field_attrs:
field_attr_value = getattr(field_class, field_attr_name, None)
if callable(field_attr_value):
if field_attr_value == EMPTY:
field_attr_value = ''
else:
try:
# try to get value from callable, without args/kwargs
field_attr_value = field_attr_value()
except:
field_attr_value = str(field_attr_value)
fields[field_code][field_attr_name] = field_attr_value
# set default values from a model data
fields[field_code]['default'] = None
if field_type not in self.default_always_null_fields:
try:
default_value = model._meta.get_field(field_code).default
if default_value != NOT_PROVIDED:
if callable(default_value):
default_value = default_value()
try:
json.dumps(default_value)
except:
default_value = str(default_value)
fields[field_code]['default'] = default_value
except:
pass
if instance:
instance_data = serializer_class(instance, many=False).data
for field_name, field_data in fields.items():
field_data['value'] = instance_data.get(field_name)
return fields
示例15: generate_altered_fields
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import NOT_PROVIDED [as 别名]
def generate_altered_fields(self):
"""
Fields that have been altered.
"""
for app_label, model_name, field_name in sorted(self.old_field_keys.intersection(self.new_field_keys)):
# Did the field change?
old_model_name = self.renamed_models.get((app_label, model_name), model_name)
old_field_name = self.renamed_fields.get((app_label, model_name, field_name), field_name)
old_field = self.old_apps.get_model(app_label, old_model_name)._meta.get_field(old_field_name)
new_field = self.new_apps.get_model(app_label, model_name)._meta.get_field(field_name)
# Implement any model renames on relations; these are handled by RenameModel
# so we need to exclude them from the comparison
if hasattr(new_field, "remote_field") and getattr(new_field.remote_field, "model", None):
rename_key = (
new_field.remote_field.model._meta.app_label,
new_field.remote_field.model._meta.model_name,
)
if rename_key in self.renamed_models:
new_field.remote_field.model = old_field.remote_field.model
if hasattr(new_field, "remote_field") and getattr(new_field.remote_field, "through", None):
rename_key = (
new_field.remote_field.through._meta.app_label,
new_field.remote_field.through._meta.model_name,
)
if rename_key in self.renamed_models:
new_field.remote_field.through = old_field.remote_field.through
old_field_dec = self.deep_deconstruct(old_field)
new_field_dec = self.deep_deconstruct(new_field)
if old_field_dec != new_field_dec:
both_m2m = old_field.many_to_many and new_field.many_to_many
neither_m2m = not old_field.many_to_many and not new_field.many_to_many
if both_m2m or neither_m2m:
# Either both fields are m2m or neither is
preserve_default = True
if (old_field.null and not new_field.null and not new_field.has_default() and
not new_field.many_to_many):
field = new_field.clone()
new_default = self.questioner.ask_not_null_alteration(field_name, model_name)
if new_default is not models.NOT_PROVIDED:
field.default = new_default
preserve_default = False
else:
field = new_field
self.add_operation(
app_label,
operations.AlterField(
model_name=model_name,
name=field_name,
field=field,
preserve_default=preserve_default,
)
)
else:
# We cannot alter between m2m and concrete fields
self._generate_removed_field(app_label, model_name, field_name)
self._generate_added_field(app_label, model_name, field_name)