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

Ability to include private or "_" fields as part of the model. #1388

Closed
mdgilene opened this issue Apr 13, 2020 · 4 comments
Closed

Ability to include private or "_" fields as part of the model. #1388

mdgilene opened this issue Apr 13, 2020 · 4 comments

Comments

@mdgilene
Copy link
Contributor

Feature Request

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.4
            pydantic compiled: False
                 install path: C:\Anaconda3_2019.10\envs\test-env\Lib\site-packages\pydantic
               python version: 3.6.8 |Anaconda, Inc.| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
                     platform: Windows-10-10.0.17134-SP0
     optional deps. installed: ['email-validator']

When building models that are meant to add typing and validation to 3rd part APIs (in this case Elasticsearch) sometimes field names are prefixed with _ however these are not private fields that should be ignored and users may want them included in the model.

from pydantic import BaseModel

class ElastisearchRequest(BaseModel):
    _source: dict = Field(...)
    
    class Config:
        include_private_fields = True

request = ElasticsearchRequest(_source={'include': ['text']})

print(request.dict())

Output:

{
  "_source": {
    "include": ["text"]
  }
}
@samuelcolvin
Copy link
Member

Duplicate of #655.

@StephenBrown2
Copy link
Contributor

With MongoDB, which has an _id field, I usually just alias it, like:

from pydantic import BaseModel, Field

class MongoResponse(BaseModel):
    id: str = Field(..., alias="_id")
    
    class Config:
        allow_population_by_field_name = True

response = MongoResponse(_id="ThisIsAnId")

print(response.dict())

Output:

{'id': 'ThisIsAnId'}

So with your example:

from pydantic import BaseModel, Field

class ElasticsearchRequest(BaseModel):
    source: dict = Field(..., alias="_source")

    class Config:
        allow_population_by_field_name = True

request = ElasticsearchRequest(_source={'include': ['text']})

print(request.dict())
print(request.dict(by_alias=True))

Output:

{'source': {'include': ['text']}}
{'_source': {'include': ['text']}}

@rafa-acioly
Copy link

@StephenBrown2 how can i use the _id as non-required using alias? because the _id is only generated once have inserted a document on my collection, using your example we cannot instantiate a new class without the _id attribute.

@StephenBrown2
Copy link
Contributor

Instead of using ... as the default, use None:

id: str = Field(None, alias="_id")

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

No branches or pull requests

4 participants