python pydantic How to choose class based on another value

149
March 28, 2022, at 11:30 AM

some of example of schema . I have different constraints based on type

short_text = {
    "text": "Some question goes here ",
    "description": "Some descritpion goes here ",
    "type": "short_text",
    "constraints": {
        "max_chars": -1,
        "required": True
    }
}
multiple_choice = {
    "text": "Some question goes here ",
    "description": "Some descritpion goes here ",
    "type": "multiple_choice",
    "constraints": {
        "multiple_selection": True,
        "required": True,
        "randomized": True,
        "other_option": True
    },
    "items": [
        {1: "choice 1"}, {2: "choice 2"}, {3: "choice 3"}
    ]
}
social_media = {
    "text": "Some question goes here ",
    "description": "Some descritpion goes here ",
    "type": "social_media",
    "constraints": {
        "platforms": [
            {
                "platform": "facebook",
                "required": True
            }, {
                "platform": "instagram",
                "required": True
            }, {
                "platform": "twitter",
                "required": True
            }, {
                "platform": "tiktok",
                "required": True
            }, {
                "platform": "snapchat",
                "required": True
            }
        ]
    }
}
multiple_choice = {
    "text": "Some question goes here ",
    "description": "Some descritpion goes here",
    "type": "multiple_choice",
    "constraints": {
        "multiple_selection": True,
        "required": True,
        "randomized": True,
        "other_option": True
    },
    "items": [
        {1: "choice 1"}, {2: "choice 2"}, {3: "choice 3"}
    ]
}

i write this

from __future__ import annotations
from pydantic import BaseModel
from typing import Union, Literal, Optional, List

# =============================== ShortText type questions =============
class ShortTextConstraints(BaseModel):
    max_chars: int
    required: bool

# =============================== multiple_choice type questions =============
class MultipleChoiceConstraints(BaseModel):
    multiple_selection: bool
    required: bool
    randomized: bool
    other_option: bool

# =============================== yes_no type questions =============
# =============================== email type questions =============
class YesNoConstraints(BaseModel):
    required: bool

# =============================== number type questions =============
class NumberConstraints(BaseModel):
    required: bool
    min_number: ValidateNumber
    max_number: ValidateNumber

class ValidateNumber(BaseModel):
    checked: bool
    value: int

# =============================== phone_number type questions =============
class PhoneNumberConstraints(BaseModel):
    required: bool
    default_country_code: str

# =============================== phone_number type questions =============
class DropdownConstraints(BaseModel):
    required: bool
    randomaize: bool
    alpabetical_order: bool

# =============================== social_media type questions =============
class SocialMediaConstraints(BaseModel):
    platforms: List[ValidatePlatforms]

class ValidatePlatforms(BaseModel):
    platform: str
    required: bool

class QuestionBase(BaseModel):
    text: str
    description: str
    type: str
    constraints: SocialMediaConstraints
    items: Optional[List[dict]]

QuestionBase.update_forward_refs()
NumberConstraints.update_forward_refs()
SocialMediaConstraints.update_forward_refs()

but in this line

  constraints: SocialMediaConstraints

automatically based on type for example

if type is short_text use ShortTextConstraints

else if type is multiple_choice use MultipleChoiceConstraints

else if type is social_media use SocialMediaConstraints

i try union for example but not work fine

Rent Charter Buses Company
READ ALSO
How to fit UMAP object with an embedding_ property of shape (200,2)

How to fit UMAP object with an embedding_ property of shape (200,2)

I'm trying to take the first 200 rows of my dataframe and pass it through a UMAP fit method but I'm not able to get it to shape the way I want at (200,2)Should I add an embedding parameter?

106
Django - passing one model to another while creating new object

Django - passing one model to another while creating new object

My site has multiple tests with multiple questions eachI'd like to make question creation form which will have preset test object depended on url

108
Import could not be resolve after installing the dependency

Import could not be resolve after installing the dependency

I'm trying to install de messagebird dependency into my python project

174
With cartopy, can a local map be rotated so that north points in an arbitrary direction?

With cartopy, can a local map be rotated so that north points in an arbitrary direction?

I have this block of python code to plot a city-scale satellite map

148