Skip to content
0:00 / 0:00

#pydantic#dataclasses#datavalidation

Do We Still Need Dataclasses? // PYDANTIC Tutorial

ArjanCodes

345K subscribers • 1 video on Hallway

YouTube
  • Recommended by 1 person: 1 on Stack Overflow and its sister sites.
  • Presented by Arjan Egges, creator of ArjanCodes
  • Linked in the descriptionArjanCodes/2021-pydantic
  • An opinion. It argues one side of a question engineers disagree on.

Intermediate. Assumes working knowledge.

213K views5 years ago1 recommendation

Programming languagesSoftware designPythonDesign patterns

Arjan Egges compares Python dataclasses with Pydantic, demonstrating how to use Pydantic for data validation and sanitization.

💡 Learn how to design great software in 7 steps: arjan.codes/designguide. Pydantic is a very useful package that makes dealing with data much easier, similar to what Python's built-in dataclasses do. Where Pydantic shines is in dealing with validating and sanitizing data. In this tutorial video, I show you how it works, and when to choose Pydantic over dataclasses in your code projects. The code that I worked on in this video is available here: github.com/ArjanCodes/2021-pydantic. 🎓 ArjanCodes Courses: arjancodes.com/courses. 💬 Join my Discord server: discord.arjan.codes. ⌨️ Keyboard I’m using: amzn.to/49YM97v. 🔖 Chapters: Intro Explaining the example Creating a Pydantic Basemodel Adding field validation Adding model-level validation More possibilities by adding a Config class Converting the model instances to a dictionary Other things that Pydantic can do So, are dataclasses obsolete? #arjancodes #softwaredesign #python

Why it's here

1 person recommends this

What people say. Recommended for its detailed examples of using Pydantic validators to validate multiple fields in a model.

Moments people linked to

wuelion Stack Overflow • Recommends

On “How to validate more than one field of a Pydantic model?”

To extend on the answer of Rahul R , this example shows in more detail how to use the pydantic validators. This example contains all the necessary information to answer your question. Note, that there is also the option to use a @root_validator , as mentioned by Kentgrav , see the example at the bottom of the post for more details. import pydantic class Parent(pydantic.BaseModel): name: str comments: str class Customer(Parent): address: str phone: str # If you want to apply the Validator to the fields "name", "comments", "address", "phone" @pydantic.validator("name", "comments", "address", "phone") @classmethod def validate_all_fields_one_by_one(cls, field_value): # Do the…
47View the answer