我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用django.db.models.BinaryField()。
def test_serializer_binary_field(self): class SomeBinaryModel(models.Model): bb = models.BinaryField() bb_empty = models.BinaryField() instance = SomeBinaryModel(bb=b'some binary data') serialized = utils.serialize_instance(instance) deserialized = utils.deserialize_instance(SomeBinaryModel, serialized) self.assertEqual(serialized['bb'], 'c29tZSBiaW5hcnkgZGF0YQ==') self.assertEqual(serialized['bb_empty'], '') self.assertEqual(deserialized.bb, b'some binary data') self.assertEqual(deserialized.bb_empty, b'')
def column_sql(self, model, field, include_default=True): db_parameter = field.db_parameters(connection=self.connection) sql = db_parameter['type'] if sql is None: return None, None if include_default: if (field.default is not None) and field.has_default(): value = field.get_default() value = self.prepare_default(value) if( djangoVersion[0:2] >= ( 1, 8 ) ): if isinstance(field,models.BinaryField ): if (value=="''"): value = 'EMPTY_BLOB()' else: value='blob( %s' %value + ')' sql += " DEFAULT %s" % value else: field.default = None if not field.null: sql += " NOT NULL" if field.primary_key: sql += " PRIMARY KEY" elif field.unique: sql += " UNIQUE" tablespace = field.db_tablespace or model._meta.db_tablespace if tablespace and field.unique: sql += " %s" % self.connection.ops.tablespace_sql(tablespace, inline=True) return sql, []
def test_is_BinaryField(self): self.assertIsInstance(EditableBinaryField(), BinaryField)
def inspect_field_names(Model): # filter out BinaryField, because it's not useful for CSV export or # live results. could be very big, and causes problems with utf-8 export # I tried .get_fields() instead of .fields, but that method returns # fields that cause problems, like saying group has an attribute 'player' return [f.name for f in Model._meta.fields if not isinstance(f, BinaryField)]