Python django.db.models.NOT_PROVIDED Examples
The following are 22
code examples of django.db.models.NOT_PROVIDED().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.db.models
, or try the search function
.
Example #1
Source File: serializers.py From yawn with MIT License | 6 votes |
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
Example #2
Source File: utils.py From django-easy-audit with GNU General Public License v3.0 | 5 votes |
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
Example #3
Source File: schema.py From python2017 with MIT License | 5 votes |
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])
Example #4
Source File: test_autodetector.py From django-sqlserver with MIT License | 5 votes |
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)
Example #5
Source File: schema.py From openhgsenti with Apache License 2.0 | 5 votes |
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])
Example #6
Source File: watcher.py From django_audit_trail with Apache License 2.0 | 5 votes |
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
Example #7
Source File: schema.py From python with Apache License 2.0 | 5 votes |
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])
Example #8
Source File: schema.py From python-mysql-pool with MIT License | 5 votes |
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])
Example #9
Source File: schema.py From Hands-On-Application-Development-with-PyCharm with MIT License | 5 votes |
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])
Example #10
Source File: test_autodetector.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
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)
Example #11
Source File: schema.py From plugin.video.netflix with MIT License | 5 votes |
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])
Example #12
Source File: schema.py From bioforum with MIT License | 5 votes |
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])
Example #13
Source File: test_autodetector.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
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)
Example #14
Source File: migrations.py From hutils with MIT License | 5 votes |
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))
Example #15
Source File: schema.py From GTDWeb with GNU General Public License v2.0 | 5 votes |
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])
Example #16
Source File: vm.py From esdc-ce with Apache License 2.0 | 5 votes |
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
Example #17
Source File: autodetector.py From python with Apache License 2.0 | 4 votes |
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)
Example #18
Source File: autodetector.py From python2017 with MIT License | 4 votes |
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)
Example #19
Source File: autodetector.py From openhgsenti with Apache License 2.0 | 4 votes |
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 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)
Example #20
Source File: mixins.py From lexpredict-contraxsuite with GNU Affero General Public License v3.0 | 4 votes |
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
Example #21
Source File: autodetector.py From bioforum with MIT License | 4 votes |
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)
Example #22
Source File: autodetector.py From GTDWeb with GNU General Public License v2.0 | 4 votes |
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)