GitHub user dosubot[bot] added a comment to the discussion: update dataset via 
api from python - CSRF token

<!-- Answer -->
There isn't a built-in Python snippet or utility in the Superset codebase that 
automatically strips disallowed fields from dataset payloads before an API 
update—validation is handled strictly by the Marshmallow schemas, and any extra 
fields will cause errors 
([schemas.py](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/datasets/schemas.py#L1-L368)).

To help, here's a Python snippet you can use to clean your payload before 
sending it:

```python
# Allowed fields for dataset and columns (from DatasetPutSchema and 
DatasetColumnsPutSchema)
DATASET_FIELDS = {
    "table_name", "database_id", "sql", "filter_select_enabled", 
"fetch_values_predicate",
    "catalog", "schema", "description", "main_dttm_col", "normalize_columns",
    "always_filter_main_dttm", "offset", "default_endpoint", "cache_timeout",
    "is_sqllab_view", "template_params", "owners", "columns", "metrics", 
"folders",
    "extra", "is_managed_externally", "external_url"
}
COLUMN_FIELDS = {
    "id", "column_name", "type", "advanced_data_type", "verbose_name", 
"description",
    "expression", "extra", "filterable", "groupby", "is_active", "is_dttm",
    "python_date_format", "uuid"
}

def clean_column(col):
    return {k: v for k, v in col.items() if k in COLUMN_FIELDS}

def clean_dataset_payload(payload):
    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 "owners" in cleaned:
        # Ensure owners is a list of integers
        cleaned["owners"] = [int(o) for o in cleaned["owners"] if isinstance(o, 
int) or (isinstance(o, str) and o.isdigit())]
    return cleaned
```

Use `clean_dataset_payload(your_payload)` before sending your PUT request. This 
will remove fields like `created_on`, `changed_on`, `type_generic`, and any 
others not accepted by the API schema, helping you avoid validation errors 
([api.py](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/datasets/api.py#L364-L447)).
 If you need to adapt this for metrics or folders, you can extend the same 
pattern.

Let me know if you want a version that handles nested metrics or other fields!


<!-- 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/2abb87ce-c8f6-48e6-bfc5-f379cf16b70f?feedback_type=great_response)
 | 
[Irrelevant](https://app.dosu.dev/response-feedback/2abb87ce-c8f6-48e6-bfc5-f379cf16b70f?feedback_type=irrelevant_answer)
 | 
[Incorrect](https://app.dosu.dev/response-feedback/2abb87ce-c8f6-48e6-bfc5-f379cf16b70f?feedback_type=incorrect_sources)
 | 
[Verbose](https://app.dosu.dev/response-feedback/2abb87ce-c8f6-48e6-bfc5-f379cf16b70f?feedback_type=too_verbose)
 | 
[Hallucination](https://app.dosu.dev/response-feedback/2abb87ce-c8f6-48e6-bfc5-f379cf16b70f?feedback_type=hallucination)
 | [Report 
šŸ›](https://app.dosu.dev/response-feedback/2abb87ce-c8f6-48e6-bfc5-f379cf16b70f?feedback_type=bug_report)
 | 
[Other](https://app.dosu.dev/response-feedback/2abb87ce-c8f6-48e6-bfc5-f379cf16b70f?feedback_type=other)</sup>

[![Chat with 
Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/a28d3c7e-a9d3-459e-9fb6-3a6f9ff4f357/ask?utm_source=github)&nbsp;[![Open
 in 
Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=There%20isn%27t%20a%20built-in%20Python%20snippet%20or%20utility%20in%20the%20Superset%20codebase%20that%20automatically%20strips%20disallowed%20fields%20from%20dataset%20payloads%20before%20an%20API%20update%E2%80%94validation%20is%20handled%20strictly%20by%20the%20Marshmallow%20schemas%2C%20and%20any%20extra%20fields%20will%20cause%20errors%20%28%5Bschemas.py%5D%28https%3A//github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/datasets/schemas.py%23L1-L368%29%29.%0A%0ATo%20help%2C%20here%27s%20a%20Python%20snippet%20you%20can%20use%20to%20clean%20your%20payload%20before%20sending%20it%3A%0A%0A%60%60%60python%0A%23%20Allowed%20fields%20for%20dataset%20and%20columns%20%28from%20DatasetPutSchema%20and%20DatasetColumnsPutSche
 
ma%29%0ADATASET_FIELDS%20%3D%20%7B%0A%20%20%20%20%22table_name%22%2C%20%22database_id%22%2C%20%22sql%22%2C%20%22filter_select_enabled%22%2C%20%22fetch_values_predicate%22%2C%0A%20%20%20%20%22catalog%22%2C%20%22schema%22%2C%20%22description%22%2C%20%22main_dttm_col%22%2C%20%22normalize_columns%22%2C%0A%20%20%20%20%22always_filter_main_dttm%22%2C%20%22offset%22%2C%20%22default_endpoint%22%2C%20%22cache_timeout%22%2C%0A%20%20%20%20%22is_sqllab_view%22%2C%20%22template_params%22%2C%20%22owners%22%2C%20%22columns%22%2C%20%22metrics%22%2C%20%22folders%22%2C%0A%20%20%20%20%22extra%22%2C%20%22is_managed_externally%22%2C%20%22external_url%22%0A%7D%0ACOLUMN_FIELDS%20%3D%20%7B%0A%20%20%20%20%22id%22%2C%20%22column_name%22%2C%20%22type%22%2C%20%22advanced_data_type%22%2C%20%22verbose_name%22%2C%20%22description%22%2C%0A%20%20%20%20%22expression%22%2C%20%22extra%22%2C%20%22filterable%22%2C%20%22groupby%22%2C%20%22is_active%22%2C%20%22is_dttm%22%2C%0A%20%20%20%20%22python_date_format%22%2C%20%22u
 
uid%22%0A%7D%0A%0Adef%20clean_column%28col%29%3A%0A%20%20%20%20return%20%7Bk%3A%20v%20for%20k%2C%20v%20in%20col.items%28%29%20if%20k%20in%20COLUMN_FIELDS%7D%0A%0Adef%20clean_dataset_payload%28payload%29%3A%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%22owners%22%20in%20cleaned%3A%0A%20%20%20%20%20%20%20%20%23%20Ensure%20owners%20is%20a%20list%20of%20integers%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%0AUse%20%60clean_dataset_payload%28your_payload%29%60%20before%20sending%20your%20PUT%20req
 
uest.%20This%20will%20remove%20fields%20like%20%60created_on%60%2C%20%60changed_on%60%2C%20%60type_generic%60%2C%20and%20any%20others%20not%20accepted%20by%20the%20API%20schema%2C%20helping%20you%20avoid%20validation%20errors%20%28%5Bapi.py%5D%28https%3A//github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/datasets/api.py%23L364-L447%29%29.%20If%20you%20need%20to%20adapt%20this%20for%20metrics%20or%20folders%2C%20you%20can%20extend%20the%20same%20pattern.%0A%0ALet%20me%20know%20if%20you%20want%20a%20version%20that%20handles%20nested%20metrics%20or%20other%20fields%21)&nbsp;[![Join
 
Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share
 on 
X](https://img.shields.io/badge/X-share-black)](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-15149777

----
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]

Reply via email to