Python django.db.models.fields.NOT_PROVIDED Examples

The following are 17 code examples of django.db.models.fields.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.fields , or try the search function .
Example #1
Source File: django.py    From prettyprinter with MIT License 5 votes vote down vote up
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),
    ) 
Example #2
Source File: serializers.py    From drf-schema-adapter with MIT License 5 votes vote down vote up
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 
Example #3
Source File: get_field_dict.py    From drf-schema-adapter with MIT License 5 votes vote down vote up
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 
Example #4
Source File: factories.py    From drf-schema-adapter with MIT License 5 votes vote down vote up
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) 
Example #5
Source File: factories.py    From drf-schema-adapter with MIT License 5 votes vote down vote up
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 
Example #6
Source File: test_operations.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
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"]) 
Example #7
Source File: test_operations.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
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"]) 
Example #8
Source File: renderers_serializers.py    From product-definition-center with MIT License 5 votes vote down vote up
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 
Example #9
Source File: test_operations.py    From django-sqlserver with MIT License 5 votes vote down vote up
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"]) 
Example #10
Source File: orm.py    From jbox with MIT License 5 votes vote down vote up
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) 
Example #11
Source File: orm.py    From googleapps-message-recall with Apache License 2.0 5 votes vote down vote up
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) 
Example #12
Source File: serviceinstance.py    From xos with Apache License 2.0 5 votes vote down vote up
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
                ) 
Example #13
Source File: serviceinstance.py    From xos with Apache License 2.0 5 votes vote down vote up
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
                ) 
Example #14
Source File: orm.py    From RSSNewsGAE with Apache License 2.0 5 votes vote down vote up
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) 
Example #15
Source File: mysql_bulk_insert.py    From mendelmd with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
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() 
Example #16
Source File: mysql_bulk_insert.py    From mendelmd with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
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() 
Example #17
Source File: mysql_bulk_insert.py    From mendelmd with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
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()