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

PatchDict util #1262

Merged
merged 2 commits into from
Aug 12, 2024
Merged

PatchDict util #1262

merged 2 commits into from
Aug 12, 2024

Conversation

vitalik
Copy link
Owner

@vitalik vitalik commented Aug 10, 2024

PatchDict container allows to convert any schema into all-optional-fields schema and return a dict with only provided fields:

Usage example

from ninja import PatchDict

class GroupSchema(Schema):
    # You do not have to make fields optional it will be converted by PatchDict
    name: str
    description: str
    due_date: date


@api.patch("/patch/{pk}")
def modify_data(request, pk: int, payload: PatchDict[GroupSchema]):
    obj = MyModel.objects.get(pk=pk)

    for attr, value in payload.items():
        setattr(obj, attr, value)
    
    obj.save()

@vitalik vitalik merged commit 50ebb85 into master Aug 12, 2024
60 checks passed
@vitalik vitalik deleted the patch-helper branch August 12, 2024 13:30
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

Successfully merging this pull request may close these issues.

1 participant