Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field order issue in JSON response #1107

Open
Zerotask opened this issue Mar 14, 2024 · 1 comment
Open

Field order issue in JSON response #1107

Zerotask opened this issue Mar 14, 2024 · 1 comment

Comments

@Zerotask
Copy link
Contributor

I noticed, that in the JSON response, the fields measurements and units are shown first, before the id, which looks weird.
I thought the order is defined in meta fields. Is this a bug or how can I enforce the order from meta fields?

class FieldTypeSchema(ModelSchema):
    measurements: list[MeasurementTypeSchema]
    units: list[UnitSchema]

    class Meta:
        model = FieldType
        fields = (
            "id",
            "key",
            *FieldType.name.fields,
            *FieldType.reference_url.fields,
            "image",
            "data_type",
            "units",
            "min_value",
            "max_value",
            "measurements",
            "attachment",
            "is_commentable",
            "related_field",
            "calculator",
        )
"field_types": [
        {
          "measurements": [],
          "units": [
            {
              "id": 18,
              "key": "quantity",
              "name_en": "...",
              "name_de": "...",
              "name_fr": "...",
              "name_es": "...",
              "name_it": "...",
            }
          ],
          "id": 274,
          "key": "...",
          "name_en": "...",
          "name_de": "...",
          "name_fr": "...",
          "name_es": "...",
          "name_it": "...",
          "reference_url_en": "",
          "reference_url_de": "",
          "reference_url_fr": "",
          "reference_url_es": "",
          "reference_url_it": "",
          "image": "",
          "data_type": "integer",
          "min_value": "",
          "max_value": "",
          "attachment": "optional",
          "is_commentable": true,
          "related_field": null,
          "calculator": null
        },
        ...
]
@vitalik
Copy link
Owner

vitalik commented Mar 14, 2024

Hi @Zerotask

well this happens somewhere inside pydantic - django ninja sends fields definitions as dict (where fields are sorted as in Meta.fields) but apparently pydantic puts attributes defined on class first

I think I raised this at pydantic issues once but did not get a solution..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants