class ExampleSerializerModel(serializers.Serializer): date = serializers.DateField() datetime = serializers.DateTimeField() hstore = serializers.HStoreField() uuid_field = serializers.UUIDField(default=uuid.uuid4) class Meta: model = OpenAPIExample
class TicketSerializer(serializers.ModelSerializer): customFields = serializers.HStoreField() class Meta: model = Ticket fields = ('id', 'processNumber', 'description', 'lowerRange', 'upperRange', 'dealValue', 'dueDate', 'status', 'customFields', 'counterparties', 'lawyers', 'defendants') + Audit.fields()
class UserSerializer(serializers.ModelSerializer): customFields = serializers.HStoreField() class Meta: model = User fields = ( 'id', 'person', 'name', 'legalId', 'username', 'customFields' ) + Audit.fields()
class FileSerializer(serializers.ModelSerializer): """ File object serializator. """ uuid = serializers.UUIDField() size = serializers.IntegerField(source='file_size', allow_null=True) url = serializers.SerializerMethodField() timestamp = TimestampField(source='created_at', required=False) type = serializers.CharField(source='file_type') metadata = serializers.HStoreField(allow_null=True, read_only=False) class Meta: model = File fields = ( 'uuid', 'orig_name', 'url', 'size', 'type', 'mime_type', 'timestamp', 'is_uploaded', 'is_compressed', 'metadata', ) @staticmethod def get_size(obj) -> int: """ Размер файла в байтах (не зависит от типа стораджа). """ return getattr(obj, 'size', getattr(obj, 'file.size', None)) @staticmethod def get_url(obj): """ Публичный URL для доступа к файлу. """ is_dict = isinstance(obj, OrderedDict) if is_dict and not obj['is_compressed']: return None if not is_dict and not obj.is_compressed: return None if isinstance(obj, OrderedDict): # Через @action сюда приходит OrderedDict - конвертим в обычный data = dict(obj) else: data = {'uuid': obj.uuid} public_url = os.path.join(settings.AWS_S3_ENDPOINT_URL, settings.AWS_STORAGE_BUCKET_NAME, str(data['uuid'])) return public_url
class MachineModelSerializer(serializers.Serializer): id = serializers.IntegerField(read_only=True) product_no = serializers.CharField(required=False, allow_blank=True, max_length=100) product_name = serializers.CharField(required=False, allow_blank=True, max_length=100) stock_quantity = serializers.IntegerField(required=False) order_date = serializers.DateTimeField() expected_arrival_date = serializers.DateTimeField() parts_used_to_make = serializers.HStoreField(child=serializers.CharField( max_length=100)) created = serializers.DateTimeField() def create(self, validated_data): """ Create and return a new `Snippet` instance, given the validated data. """ return MachineModel.objects.create(**validated_data) def update(self, instance, validated_data): """ Update and return an existing `Snippet` instance, given the validated data. """ instance.product_no = validated_data.get('product_no', instance.product_no) instance.product_name = validated_data.get('product_name', instance.product_name) instance.stock_quantity = validated_data.get('stock_quantity', instance.stock_quantity) instance.order_date = validated_data.get('order_date', instance.order_date) instance.expected_arrival_date = validated_data.get( 'expected_arrival_date', instance.expected_arrival_date) instance.save() return instance
class ExampleSerializer(serializers.Serializer): date = serializers.DateField() datetime = serializers.DateTimeField() hstore = serializers.HStoreField()
class ExampleSerializer(serializers.Serializer): date = serializers.DateField() datetime = serializers.DateTimeField() hstore = serializers.HStoreField() uuid_field = serializers.UUIDField(default=uuid.uuid4)