-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Add public BaseModel.iter() method #997
Comments
This already exists! It's called |
Should be documented I guess. |
Yeah, but I mentioned, why it cannot be used in this case:
By some work i mean resolving aliases keys and excluding the values |
I don't want to add more public method to models, best I think to implement this yourself. If there's a simple way to achieve this with changes to existing functions of happy review a PR. |
Sorry, I missed your comment when replying in a hurry. |
Just to be clear, thats valid even if
I guess the simplest way would be to move those excluding and aliases stuff from I'll try to play with it soon and will be happy to make a PR once it'll be done. |
For what it's worth, I would be in favor of moving logic from |
On V2, we can leverage from pydantic import BaseModel
class Foo(BaseModel):
a: str
b: int
foo = Foo(a="a", b=1)
for key, value in foo.model_dump().items():
print(key, value) For that reason, I'll be closing this issue. 🙏 |
@Kludex import bson
from pydantic import BaseModel, Field
from pydantic_core import core_schema
class PydanticObjectId(bson.ObjectId):
@classmethod
def __get_pydantic_core_schema__(cls, source_type, handler):
serialization = core_schema.plain_serializer_function_ser_schema(str)
return core_schema.is_instance_schema(cls, serialization=serialization)
class Foo(BaseModel):
id: PydanticObjectId = Field(default_factory=PydanticObjectId)
b: int
foo = Foo(b=1)
print(f"str: {foo}")
print(f"_iter: {dict(foo._iter())}")
print(f"model_dump: {foo.model_dump()}") Output:
So unless there is a way to prevent custom serialization, I'd suggest to reopen this issue. |
no longer really relevant in V2 - |
An example of how the previous example would work by using pydantic-core methods instead of |
I think there is a problem with your
|
So what was the final verdict? Can we use |
Feature Request
Add
BaseModel.iter()
method which will behave exactly like a dict, but without converting nested values to dict by default and will be a generator insteadWhy
Imagine you want to iterate through values in model, but also need to exclude some, or use aliases instead of attributes. So what you need to do is write something like this:
Obviously, in this case python will firet evaluate
model.dict(exclude={...}, by_alias=True).
part, going through all values and only then you will be able to iterate, going through the same values again.Also, by using dict nested values are force-converted to dict, and this might be very slow when we just want to iterate through top-level values.
We also cannot use existing
_iter()
, because some work is actually done indict()
How we can solve it
By having
iter()
method we can bypass this double-iteration problem and converting-to-dict problems:Selected Assignee: @hramezani
The text was updated successfully, but these errors were encountered: