當前位置: 首頁>>代碼示例>>Python>>正文


Python models.NOT_PROVIDED屬性代碼示例

本文整理匯總了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 
開發者ID:aclowes,項目名稱:yawn,代碼行數:20,代碼來源:serializers.py

示例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]) 
開發者ID:LuciferJack,項目名稱:python-mysql-pool,代碼行數:13,代碼來源:schema.py

示例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]) 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:12,代碼來源:schema.py

示例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)) 
開發者ID:zaihui,項目名稱:hutils,代碼行數:11,代碼來源:migrations.py

示例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]) 
開發者ID:reBiocoder,項目名稱:bioforum,代碼行數:13,代碼來源:schema.py

示例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]) 
開發者ID:CastagnaIT,項目名稱:plugin.video.netflix,代碼行數:13,代碼來源:schema.py

示例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 
開發者ID:soynatan,項目名稱:django-easy-audit,代碼行數:28,代碼來源:utils.py

示例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]) 
開發者ID:Yeah-Kun,項目名稱:python,代碼行數:13,代碼來源:schema.py

示例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) 
開發者ID:denisenkom,項目名稱:django-sqlserver,代碼行數:13,代碼來源:test_autodetector.py

示例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 
開發者ID:TriplePoint-Software,項目名稱:django_audit_trail,代碼行數:36,代碼來源:watcher.py

示例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 
開發者ID:erigones,項目名稱:esdc-ce,代碼行數:6,代碼來源:vm.py

示例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) 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:57,代碼來源:autodetector.py

示例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) 
開發者ID:reBiocoder,項目名稱:bioforum,代碼行數:59,代碼來源:autodetector.py

示例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 
開發者ID:LexPredict,項目名稱:lexpredict-contraxsuite,代碼行數:50,代碼來源:mixins.py

示例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) 
開發者ID:Yeah-Kun,項目名稱:python,代碼行數:58,代碼來源:autodetector.py


注:本文中的django.db.models.NOT_PROVIDED屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。