GitHub user dosubot[bot] added a comment to the discussion: update dataset via api from python - CSRF token
<!-- Answer --> Here's an updated cleaning snippet for metrics. The allowed fields for metrics are: id, expression, description, extra, metric_name, metric_type, d3format, currency, verbose_name, warning_text, and uuid. For the nested currency field, only symbol and symbolPosition are allowed. These are defined in the Superset codebase's DatasetMetricsPutSchema and DatasetMetricCurrencyPutSchema ([source](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/datasets/schemas.py#L77-L163)). ```python # Allowed fields for metrics and nested currency METRIC_FIELDS = { "id", "expression", "description", "extra", "metric_name", "metric_type", "d3format", "currency", "verbose_name", "warning_text", "uuid" } CURRENCY_FIELDS = {"symbol", "symbolPosition"} def clean_currency(currency): if not isinstance(currency, dict): return None return {k: v for k, v in currency.items() if k in CURRENCY_FIELDS} def clean_metric(metric): cleaned = {k: v for k, v in metric.items() if k in METRIC_FIELDS} if "currency" in cleaned: cleaned["currency"] = clean_currency(cleaned["currency"]) return cleaned def clean_dataset_payload(payload): # ... your existing DATASET_FIELDS, COLUMN_FIELDS, clean_column ... cleaned = {k: v for k, v in payload.items() if k in DATASET_FIELDS} if "columns" in cleaned: cleaned["columns"] = [clean_column(col) for col in cleaned["columns"]] if "metrics" in cleaned: cleaned["metrics"] = [clean_metric(m) for m in cleaned["metrics"]] if "owners" in cleaned: cleaned["owners"] = [int(o) for o in cleaned["owners"] if isinstance(o, int) or (isinstance(o, str) and o.isdigit())] return cleaned ``` This will ensure your metrics field is API-compliant and avoids marshmallow validation errors. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/f8400902-a68e-4092-9b3c-b6fda4d990c7?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/f8400902-a68e-4092-9b3c-b6fda4d990c7?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/f8400902-a68e-4092-9b3c-b6fda4d990c7?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/f8400902-a68e-4092-9b3c-b6fda4d990c7?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/f8400902-a68e-4092-9b3c-b6fda4d990c7?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/f8400902-a68e-4092-9b3c-b6fda4d990c7?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/f8400902-a68e-4092-9b3c-b6fda4d990c7?feedback_type=other)</sup> [](https://app.dosu.dev/a28d3c7e-a9d3-459e-9fb6-3a6f9ff4f357/ask?utm_source=github) [](https://cursor.com/link/prompt?text=Here%27s%20an%20updated%20cleaning%20snippet%20for%20metrics.%20The%20allowed%20fields%20for%20metrics%20are%3A%20id%2C%20expression%2C%20description%2C%20extra%2C%20metric_name%2C%20metric_type%2C%20d3format%2C%20currency%2C%20verbose_name%2C%20warning_text%2C%20and%20uuid.%20For%20the%20nested%20currency%20field%2C%20only%20symbol%20and%20symbolPosition%20are%20allowed.%20These%20are%20defined%20in%20the%20Superset%20codebase%27s%20DatasetMetricsPutSchema%20and%20DatasetMetricCurrencyPutSchema%20%28%5Bsource%5D%28https%3A//github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/datasets/schemas.py%23L77-L163%29%29.%0A%0A%60%60%60python%0A%23%20Allowed%20fields%20for%20metrics%20and%20nested%20currency%0AMETRIC_FIELDS%20%3D% 20%7B%0A%20%20%20%20%22id%22%2C%20%22expression%22%2C%20%22description%22%2C%20%22extra%22%2C%20%22metric_name%22%2C%20%22metric_type%22%2C%0A%20%20%20%20%22d3format%22%2C%20%22currency%22%2C%20%22verbose_name%22%2C%20%22warning_text%22%2C%20%22uuid%22%0A%7D%0ACURRENCY_FIELDS%20%3D%20%7B%22symbol%22%2C%20%22symbolPosition%22%7D%0A%0Adef%20clean_currency%28currency%29%3A%0A%20%20%20%20if%20not%20isinstance%28currency%2C%20dict%29%3A%0A%20%20%20%20%20%20%20%20return%20None%0A%20%20%20%20return%20%7Bk%3A%20v%20for%20k%2C%20v%20in%20currency.items%28%29%20if%20k%20in%20CURRENCY_FIELDS%7D%0A%0Adef%20clean_metric%28metric%29%3A%0A%20%20%20%20cleaned%20%3D%20%7Bk%3A%20v%20for%20k%2C%20v%20in%20metric.items%28%29%20if%20k%20in%20METRIC_FIELDS%7D%0A%20%20%20%20if%20%22currency%22%20in%20cleaned%3A%0A%20%20%20%20%20%20%20%20cleaned%5B%22currency%22%5D%20%3D%20clean_currency%28cleaned%5B%22currency%22%5D%29%0A%20%20%20%20return%20cleaned%0A%0Adef%20clean_dataset_payload%28payload%29%3A%0A%20%2 0%20%20%23%20...%20your%20existing%20DATASET_FIELDS%2C%20COLUMN_FIELDS%2C%20clean_column%20...%0A%20%20%20%20cleaned%20%3D%20%7Bk%3A%20v%20for%20k%2C%20v%20in%20payload.items%28%29%20if%20k%20in%20DATASET_FIELDS%7D%0A%20%20%20%20if%20%22columns%22%20in%20cleaned%3A%0A%20%20%20%20%20%20%20%20cleaned%5B%22columns%22%5D%20%3D%20%5Bclean_column%28col%29%20for%20col%20in%20cleaned%5B%22columns%22%5D%5D%0A%20%20%20%20if%20%22metrics%22%20in%20cleaned%3A%0A%20%20%20%20%20%20%20%20cleaned%5B%22metrics%22%5D%20%3D%20%5Bclean_metric%28m%29%20for%20m%20in%20cleaned%5B%22metrics%22%5D%5D%0A%20%20%20%20if%20%22owners%22%20in%20cleaned%3A%0A%20%20%20%20%20%20%20%20cleaned%5B%22owners%22%5D%20%3D%20%5Bint%28o%29%20for%20o%20in%20cleaned%5B%22owners%22%5D%20if%20isinstance%28o%2C%20int%29%20or%20%28isinstance%28o%2C%20str%29%20and%20o.isdigit%28%29%29%5D%0A%20%20%20%20return%20cleaned%0A%60%60%60%0A%0AThis%20will%20ensure%20your%20metrics%20field%20is%20API-compliant%20and%20avoids%20marshmallow%20 validation%20errors.) [](https://go.dosu.dev/discord-bot) [](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/apache/superset/discussions/36409) GitHub link: https://github.com/apache/superset/discussions/36409#discussioncomment-15149869 ---- This is an automatically sent email for [email protected]. To unsubscribe, please send an email to: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
