Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

marshmallow_dataclass scheme map to flask api.model #90

Closed
c0x65o opened this issue Jun 17, 2020 · 2 comments
Closed

marshmallow_dataclass scheme map to flask api.model #90

c0x65o opened this issue Jun 17, 2020 · 2 comments
Labels
question Further information is requested

Comments

@c0x65o
Copy link

c0x65o commented Jun 17, 2020

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)

@lovasoa lovasoa added the question Further information is requested label Jun 18, 2020
@lovasoa
Copy link
Owner

lovasoa commented Jun 18, 2020

Hello,
You are not mentioning it, but I guess what you call a flask namespace model is actually a flask-restplus model, right ?
Marshmallow-dataclass generates (as its name suggests) marshmallow schemas. So what you are looking for is actually a way to use marshmallow schemas with create flask_restplus. A quick google search for that gives me this result: https://github.com/joeyorlando/flask-restplus-marshmallow

@lovasoa lovasoa closed this as completed Jun 18, 2020
@c0x65o
Copy link
Author

c0x65o commented Jun 18, 2020

I am using flask-restx. I found this discussion:
python-restx/flask-restx#59

So I am guessing their isn't a current way to use Marshmallow-dataclass with flask-restx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants