class BoundarySerializer(GeoModelSerializer): label = serializers.CharField(max_length=128, allow_blank=False) color = serializers.CharField(max_length=64, required=False) display_field = serializers.CharField(max_length=10, allow_blank=True, required=False) data_fields = JsonBField(read_only=True, allow_null=True) errors = JsonBField(read_only=True, allow_null=True) def create(self, validated_data): boundary = super(BoundarySerializer, self).create(validated_data) boundary.load_shapefile() return boundary class Meta: model = Boundary # These meta read_only/exclude settings only apply to the fields the ModelSerializer # instantiates for you by default. If you override a field manually, you need to override # all settings there. # e.g. adding 'errors' to this tuple has no effect, since we manually define the errors # field above read_only_fields = ( 'uuid', 'status', ) fields = '__all__'
class RecordSerializer(GeoModelSerializer): data = JsonBField() class Meta: model = Record fields = '__all__' read_only_fields = ('uuid', )
class BoundaryPolygonNoGeomSerializer(ModelSerializer): """Serialize a BoundaryPolygon without any geometry info""" data = JsonBField() bbox = GeomBBoxField(source='geom') class Meta: model = BoundaryPolygon exclude = ('geom', )
class BoundaryPolygonSerializer(GeoFeatureModelSerializer): data = JsonBField() class Meta: model = BoundaryPolygon geo_field = 'geom' id_field = 'uuid' exclude = ('boundary', )
class SavedFilterSerializer(ModelSerializer): # This is a bit of a misnomer since the model field is a JSONField, but there is # no difference between JSON and JSONB at the serialization level; both are represented # as dictionaries. filter_json = JsonBField() class Meta: model = SavedFilter read_only_fields = ('uuid', 'owner') fields = '__all__'
def test_allow_null_jsonb_field(self): """ Ensure falsy values convert to None, except empty objects/arrays """ null_jsonb_field = JsonBField(allow_null=True) to_value = null_jsonb_field.to_internal_value(None) self.assertEqual(to_value, None) to_value = null_jsonb_field.to_internal_value('') self.assertEqual(to_value, None) to_value = null_jsonb_field.to_internal_value({}) self.assertEqual(to_value, {}) to_value = null_jsonb_field.to_internal_value([]) self.assertEqual(to_value, [])
class SerializerJsonBFieldTestCase(TestCase): """ Test serializer field for JsonB """ def setUp(self): self.jsonb_field = JsonBField() self.valid = {"name": "Eggs", "price": 34.99} def test_to_representation(self): """ Pass through internal dict to dict, no transformation necessary """ to_value = self.jsonb_field.to_representation(self.valid) self.assertEqual(to_value, self.valid) to_value = self.jsonb_field.to_representation(3) self.assertEqual(to_value, 3) to_value = self.jsonb_field.to_representation(None) self.assertEqual(to_value, None) to_value = self.jsonb_field.to_representation([1, 2, 3]) self.assertEqual(to_value, [1, 2, 3]) def test_to_internal_value(self): """ Check valid json passed when converting list and dict are valid, string/int/null invalid """ to_value = self.jsonb_field.to_internal_value(self.valid) self.assertEqual(to_value, self.valid) to_value = self.jsonb_field.to_internal_value([]) self.assertEqual(to_value, []) with self.assertRaises(ValidationError): self.jsonb_field.to_internal_value(None) with self.assertRaises(ValidationError): self.jsonb_field.to_internal_value(3) with self.assertRaises(ValidationError): self.jsonb_field.to_internal_value('invalid') def test_allow_null_jsonb_field(self): """ Ensure falsy values convert to None, except empty objects/arrays """ null_jsonb_field = JsonBField(allow_null=True) to_value = null_jsonb_field.to_internal_value(None) self.assertEqual(to_value, None) to_value = null_jsonb_field.to_internal_value('') self.assertEqual(to_value, None) to_value = null_jsonb_field.to_internal_value({}) self.assertEqual(to_value, {}) to_value = null_jsonb_field.to_internal_value([]) self.assertEqual(to_value, [])
def setUp(self): self.jsonb_field = JsonBField() self.valid = {"name": "Eggs", "price": 34.99}