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

[BUG] #1133

Open
medram opened this issue Apr 19, 2024 · 1 comment
Open

[BUG] #1133

medram opened this issue Apr 19, 2024 · 1 comment

Comments

@medram
Copy link

medram commented Apr 19, 2024

Describe the bug
Hello guys, the option exclude_unset=True isn't working for me for some reason:

Versions (please complete the following information):

  • Python version: 3.12.1
  • Django version: 5.0.1
  • Django-Ninja version: 1.1.0
  • Pydantic version: 2.6.4

Here is the snippet code:

class ContractSchemaInput(ModelSchema):
    sender_id: int
    client_id: int
    type_id: int
    price_frequency: Literal["minute", "hourly", "daily", "weekly", "monthly"]
    care_type: Literal["​ambulante", "accommodation"]
    attachment_ids: list[str] = []

    class Meta:
        model = Contract
        exclude = ("id", "type", "sender", "client", "updated", "created")

@router.patch("/contracts/{int:id}/update", response=ContractSchema)
def update_client_contract(request: HttpRequest, id: int, contract: ContractSchemaInput):
    print("Payload:", contract.dict(exclude_unset=True))
    Contract.objects.filter(id=id).update(**contract.dict(exclude_unset=True))

    return get_object_or_404(Contract, id=id)

Output error:

{
  "detail": [
    {
      "type": "missing",
      "loc": [
        "body",
        "contract",
        "sender_id"
      ],
      "msg": "Field required"
    },
    {
      "type": "missing",
      "loc": [
        "body",
        "contract",
        "client_id"
      ],
      "msg": "Field required"
    },
    {
      "type": "missing",
      "loc": [
        "body",
        "contract",
        "type_id"
      ],
      "msg": "Field required"
    },
    {
      "type": "missing",
      "loc": [
        "body",
        "contract",
        "care_type"
      ],
      "msg": "Field required"
    },
    {
      "type": "missing",
      "loc": [
        "body",
        "contract",
        "start_date"
      ],
      "msg": "Field required"
    },
    {
      "type": "missing",
      "loc": [
        "body",
        "contract",
        "end_date"
      ],
      "msg": "Field required"
    },
    {
      "type": "missing",
      "loc": [
        "body",
        "contract",
        "care_name"
      ],
      "msg": "Field required"
    }
  ]
}

Please any suggestions?

@pmdevita
Copy link

pmdevita commented Apr 19, 2024

Your fields are all marked as required, you'll need to set a default to make them not. Basically the opposite of https://docs.pydantic.dev/latest/concepts/models/#field-ordering also https://django-ninja.dev/guides/response/django-pydantic/#making-fields-optional

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