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


Python fields.NOT_PROVIDED屬性代碼示例

本文整理匯總了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) 
開發者ID:jpush,項目名稱:jbox,代碼行數:16,代碼來源:orm.py

示例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
                ) 
開發者ID:open-cloud,項目名稱:xos,代碼行數:41,代碼來源:serviceinstance.py

示例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) 
開發者ID:google,項目名稱:googleapps-message-recall,代碼行數:15,代碼來源:orm.py

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

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

示例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 
開發者ID:drf-forms,項目名稱:drf-schema-adapter,代碼行數:11,代碼來源:factories.py

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

示例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 
開發者ID:drf-forms,項目名稱:drf-schema-adapter,代碼行數:7,代碼來源:get_field_dict.py

示例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 
開發者ID:drf-forms,項目名稱:drf-schema-adapter,代碼行數:7,代碼來源:serializers.py

示例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() 
開發者ID:raonyguimaraes,項目名稱:mendelmd,代碼行數:44,代碼來源:mysql_bulk_insert.py


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