Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Comments

Encoding subclass of a model with json_encoders#1769

Merged
tiangolo merged 2 commits intofastapi:masterfrom
henrybetts:json-encoders-model-subclass
Aug 3, 2020
Merged

Encoding subclass of a model with json_encoders#1769
tiangolo merged 2 commits intofastapi:masterfrom
henrybetts:json-encoders-model-subclass

Conversation

@henrybetts
Copy link
Contributor

Currently when encoding a model, thejson_encoders property is ignored if it is defined in a superclass of the model. For example;

frompydanticimportBaseModelfromdatetimeimportdatetime,timezonefromfastapi.encodersimportjsonable_encoderclassModelWithCustomEncoder(BaseModel):classConfig:json_encoders= {datetime:lambdadt:dt.replace(microsecond=0,tzinfo=timezone.utc            ).isoformat()        }classModelWithCustomEncoderSubclass(ModelWithCustomEncoder):dt_field:datetimeclassConfig:passmodel=ModelWithCustomEncoderSubclass(dt_field=datetime(2019,1,1,8))print(model.json())# {"dt_field": "2019-01-01T08:00:00+00:00"}print(jsonable_encoder(model))# {'dt_field': '2019-01-01T08:00:00'}

To fix this, I modifiedjsonable_encoder to get the config using the.__config__ property.

@codecov
Copy link

codecovbot commentedJul 22, 2020

Codecov Report

Merging#1769 intomaster willnot change coverage.
The diff coverage is100.00%.

Impacted file tree graph

@@            Coverage Diff            @@##            master     #1769   +/-   ##=========================================  Coverage   100.00%   100.00%           =========================================  Files          235       235             Lines         6989      6995    +6     =========================================+ Hits          6989      6995    +6
Impacted FilesCoverage Δ
fastapi/encoders.py100.00% <100.00%> (ø)
tests/test_jsonable_encoder.py100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend -Click here to learn more
Δ = absolute <relative> (impact),ø = not affected,? = missing data
Powered byCodecov. Last updatea63b1ef...53e1185. Read thecomment docs.

@github-actions
Copy link
Contributor

📝 Docs preview for commit53e1185 at:https://5f183a82083ba939380c4c42--fastapi.netlify.app

@tiangolotiangolo merged commit7fbe373 intofastapi:masterAug 3, 2020
@tiangolo
Copy link
Member

Excellent, thank you! 🚀 🎉

henrybetts reacted with hooray emoji

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@henrybetts@tiangolo

[8]ページ先頭

©2009-2026 Movatter.jp