Skip to content

marshmallow_dataclass scheme map to flask api.model #90

Closed
@c0x65o

Description

@c0x65o

Is it possible to map a marshmallow_dataclass to a flask Namespace model?

IPv4 = NewType(
    "IPv4", str, validate=marshmallow.validate.Regexp(r"^([0-9]{1,3}\\.){3}[0-9]{1,3}$")
)

@dataclass(order=True)
class Project:
    name: str = field(
        metadata=dict(description="The name of the project", load_only=True)
    )
    path: str
    title: str
    ip: IPv4
    created_date: datetime = datetime.now()
    Schema: ClassVar[Type[Schema]] = Schema 

I don't want to have to re-write these:

project = api.model('Project', fields(Project))

project = api.model('Project', {
    'path': fields.String(required=True, description='The path to the project'),
    'name': fields.String(required=True, description='The name of the project'),
    'title': fields.String(required=True, description='The title of the project'),
    'ip': fields.String(required=True, description='An ip address of the project (e.g 10.10.0.1)'),
})

Then I would want to do something like @api.marshal_list_with(Project)

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions