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

Schema is loading deferred properties #932

Open
vitalik opened this issue Nov 17, 2023 Discussed in #928 · 6 comments
Open

Schema is loading deferred properties #932

vitalik opened this issue Nov 17, 2023 Discussed in #928 · 6 comments
Assignees

Comments

@vitalik
Copy link
Owner

vitalik commented Nov 17, 2023

Discussed in #928

Originally posted by mustafa0x November 17, 2023

class File(Schema):
    id: int
    text: str | None = None

@router.get('/file/{file_id}/', response=File, exclude_unset=True)
def get_file(request: ASGIRequest, file_id: int):
    return File.objects.only(('id',)).get(id=file_id)

The response includes the text, even though I excluded/deferred it using only.

@mustafa0x
Copy link

Sorry @vitalik, it seems like things are indeed working properly. This can be closed!

@mustafa0x
Copy link

mustafa0x commented Nov 20, 2023

Ok, I apologize again, things are indeed not working as originally reported.

The following line is what triggers instance.refresh_from_db(fields=[field_name]) in django.

value = getattr(self._obj, key)

@mustafa0x
Copy link

mustafa0x commented Nov 20, 2023

Something like this should be a good solution. It might need to be made opt-in, to avoid a breaking change.

if key in self._obj.get_deferred_fields():
    # skip field

@mustafa0x
Copy link

Similar discussion: pydantic/pydantic#6861

@vitalik vitalik self-assigned this Nov 21, 2023
@mustafa0x
Copy link

pydantic/pydantic#8192

Lazy loaded orm fields are loaded inadvertently by model_validate. I'd like to be able to exclude fields from model_validate.

@mustafa0x
Copy link

mustafa0x commented Dec 12, 2023

This overlaps with #333.

Also, I filed an issue in pydantic core
pydantic/pydantic-core#1095

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