Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How come FORMAT_FIELD_NAMES isn't recursive on dictionaries?#1057

Answeredbysliverc
jimbofreedman asked this question inQ&A
Discussion options

(I saw some other discussions that seem tangentially related to this but regarding OpenAPI)

Looking at the tests aroundJSON_API_FORMAT_FIELD_NAMES, the test cases are:

@pytest.mark.parametrize(    "format_type,output",    [        (False, {"full_name": {"last_name": "a", "first_name": "b"}}),        ("camelize", {"fullName": {"lastName": "a", "firstName": "b"}}),        ("capitalize", {"FullName": {"LastName": "a", "FirstName": "b"}}),        ("dasherize", {"full-name": {"last-name": "a", "first-name": "b"}}),        ("underscore", {"full_name": {"last_name": "a", "first_name": "b"}}),    ],)

With the subobject included in the test but not formatted, it looks to me like it's a conscious choice not to format subobject field names in the same way. May I ask why that is?

[edit]test_parse_preserves_json_value_field_names has given me a hint - wouldn't want totaint any third-party objects being included. Do you think some configuration flexibility might be possible here? e.g.JSON_API_FORMAT_FIELD_NAMES_RECURSIVE or something?

You must be logged in to vote

You can find a detailed explanation in PR#420. In short only the field name is part of the JSON:API specification but the value (a value might be nested json) should remain untouched.

For me an optionJSON_API_FORMAT_FIELD_NAMES_RECURSIVE does not make sense in the context of DJA as the value is not specified how it needs to be formatted and therefore needs to be kept untouched by the DJA framework.

What you can do is though to overwriteto_internal_value andto_representation either of a nested serializer, aDictField orJSONField. where you then callutils.format_field_names recursively.

Replies: 1 comment 1 reply

Comment options

You can find a detailed explanation in PR#420. In short only the field name is part of the JSON:API specification but the value (a value might be nested json) should remain untouched.

For me an optionJSON_API_FORMAT_FIELD_NAMES_RECURSIVE does not make sense in the context of DJA as the value is not specified how it needs to be formatted and therefore needs to be kept untouched by the DJA framework.

What you can do is though to overwriteto_internal_value andto_representation either of a nested serializer, aDictField orJSONField. where you then callutils.format_field_names recursively.

You must be logged in to vote
1 reply
@jimbofreedman
Comment options

Thanks for the pointer. I guess I got confused because the behaviour differs from stock DRF, but it definitely makes sense within the bounds of the JSPON:API spec. I'll find another way!

Answer selected bysliverc
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@jimbofreedman@sliverc

[8]ページ先頭

©2009-2025 Movatter.jp