Пример #1
0
    def load(self, data, *, many=None, partial=None, unknown=None, **kwargs):
        # When being passed in via the query string, we may get the raw JSON string instead of
        # the deserialized dictionary. We need to unpack it ourselves.
        if isinstance(data, str):
            try:
                data = json.loads(data)
            except json.decoder.JSONDecodeError as exc:
                raise ValidationError({
                    "_schema": [
                        f"Invalid JSON value: '{data}'",
                        str(exc),
                    ],
                })
        elif isinstance(data, QueryExpression):
            return data

        if not self.context or "table" not in self.context:
            raise RuntimeError(f"No table in context for field {self}")

        if not data:
            return NothingExpression()

        try:
            tree_to_expr(data, self.context["table"])
        except ValueError as e:
            raise ValidationError(str(e)) from e
        return super().load(data,
                            many=many,
                            partial=partial,
                            unknown=unknown,
                            **kwargs)
Пример #2
0
 def _load(self, value, data, partial=None):
     _data = super()._load(value, data, partial=partial)
     return tree_to_expr(_data, table=self.metadata["table"])