Pydantic - Nested Models, JSON Schema and Auto-Generating Models with datamodel-code-generator

Поділитися
Вставка
  • Опубліковано 20 чер 2024
  • In this video, we dive further into the Pydantic library in Python. This is based on the following blog post:
    bugbytes.io/posts/pydantic-ne...
    We'll learn a number of things, including:
    * How to define nested Pydantic models, forming parent-child relationships that allow us to model complex data that contains relationships.
    * Using typing.Literal to constrain values on a field
    * Defining default values for fields
    * Generating JSON Schema from Pydantic models
    * Auto-generating Pydantic models from JSON Schema definitions
    Github data: github.com/bugbytes-io/datasets
    📌 𝗖𝗵𝗮𝗽𝘁𝗲𝗿𝘀:
    00:00 Intro
    03:47 Adding new Pydantic model for nested data
    05:19 Adding a link to nested model in parent Pydantic class
    07:38 The effect of the Union type on a Pydantic field
    10:29 Constraining field values with Python Literal type
    12:53 Adding custom validator function to check list length
    15:05 JSON Schema outputs with Pydantic models
    18:46 Auto-generating Pydantic models from JSON Schema with datamodel-code-generator
    ☕️ 𝗕𝘂𝘆 𝗺𝗲 𝗮 𝗰𝗼𝗳𝗳𝗲𝗲:
    To support the channel and encourage new videos, please consider buying me a coffee here:
    ko-fi.com/bugbytes
    ▶️ Full Playlist:
    • Pydantic
    𝗦𝗼𝗰𝗶𝗮𝗹 𝗠𝗲𝗱𝗶𝗮:
    📖 Blog: bugbytes.io/posts/pydantic-ne...
    👾 Github: github.com/bugbytes-io/datasets
    🐦 Twitter: / bugbytesio
    📚 𝗙𝘂𝗿𝘁𝗵𝗲𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 𝗮𝗻𝗱 𝗶𝗻𝗳𝗼𝗿𝗺𝗮𝘁𝗶𝗼𝗻:
    Pydantic Blog post: www.bugbytes.io/posts/pydanti...
    Github Dataset: github.com/bugbytes-io/datasets
    Pydantic Models: docs.pydantic.dev/usage/models/
    Pydantic Validators: docs.pydantic.dev/usage/valid...
    Pydantic Schema: docs.pydantic.dev/usage/schema/
    JSON Schema: json-schema.org/
    #python #pydantic #datascience

КОМЕНТАРІ • 26

  • @silkogelman
    @silkogelman Рік тому +5

    Thanks for another great Pydantic tutorial Lyle! 🙏😀
    Great tip about possible undesired conversion behaviour at 8:38 and the order of union types.
    I'm looking forward to the next one.

    • @bugbytes3923
      @bugbytes3923  Рік тому +1

      Thank you Sil! Glad that tip was useful.

  • @zeinabmeftah
    @zeinabmeftah Рік тому +1

    Great Lesson
    Thanx
    the typing.Literal tip was very helpful for me .

    • @bugbytes3923
      @bugbytes3923  Рік тому

      No problem, and thank you for watching!

  • @seydinaoumarsamabaly1806
    @seydinaoumarsamabaly1806 Рік тому +1

    Thank you for the effort and just thank you mate 🙌🏾 great tutorial again as usual !

  • @JustinSpryce
    @JustinSpryce Рік тому +1

    Great lesson

  • @mrrobinhood5
    @mrrobinhood5 11 місяців тому +1

    How do you not have more views?! This is great content

    • @bugbytes3923
      @bugbytes3923  11 місяців тому

      Thanks a lot, glad to hear that! Thanks for watching.

  • @MdHasan-kh4dq
    @MdHasan-kh4dq 5 місяців тому

    This is the best explanation. Your tutorial is very helpful for me to understand the topic clearly. I would love to watch a series of FastAPI with Pydantic from you.

    • @bugbytes3923
      @bugbytes3923  5 місяців тому

      Thanks a lot! I have started creating such a playlist on FastAPI and Pydantic here:
      ua-cam.com/play/PL-2EBeDYMIbQghmnb865lpdmYyWU3I5F1.html

  • @mohammedbahnasy9870
    @mohammedbahnasy9870 3 місяці тому

    Thank you very much ❤❤

  • @vivaldi-qo7xj
    @vivaldi-qo7xj Рік тому +1

    thnx

    • @bugbytes3923
      @bugbytes3923  Рік тому

      You're welcome, thank you for watching.

  • @ShawnMorel
    @ShawnMorel 5 місяців тому +1

    FYI `schema_json` is deprecated in pydantic v2 and replaced with `model_json_schema`

    • @bugbytes3923
      @bugbytes3923  5 місяців тому

      Thanks for sharing! Need to update these now that v2 is out.

  • @DreamsAPI
    @DreamsAPI 9 місяців тому

    Hi Cool video, can you share the link to the video for the third video in this series? Thank you for making them.

    • @DreamsAPI
      @DreamsAPI 9 місяців тому

      ua-cam.com/video/Z0a0Vjd992I/v-deo.html&ab_channel=BugBytes

    • @bugbytes3923
      @bugbytes3923  9 місяців тому

      Thanks very much! The link for the next video is:
      ua-cam.com/video/Z0a0Vjd992I/v-deo.html

    • @DreamsAPI
      @DreamsAPI 9 місяців тому

      @@bugbytes3923 Gracias

  • @dmitrymikhailovnicepianomu8688

    Unbelievable

  • @malteplath
    @malteplath 6 місяців тому

    5:40 - please don't assign a list (or any mutable type) as a default value. Even if Pydantic probably handles this gracefully, it is a trap for users of the class if they construct Student objects and use append to build the list of modules. Use the field helper class instead.

    • @bugbytes3923
      @bugbytes3923  6 місяців тому +1

      Non-hashable values can safely be used in Pydantic as defaults:
      docs.pydantic.dev/latest/concepts/models/#fields-with-non-hashable-default-values