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

Validating List of DictType #592

Open
bencarter78 opened this issue Mar 21, 2019 · 1 comment
Open

Validating List of DictType #592

bencarter78 opened this issue Mar 21, 2019 · 1 comment

Comments

@bencarter78
Copy link

python = 3.7.0
version = '2.1.0'

Hello

I'm getting an error when trying to validate of list of dicts.

class LearningAims(Model):
    aims = ListType(DictType(StringType), required=True)

data = [{"name": "foo"}, {"name": "bar"}]

LearningAims(data).validate()

The error is TypeError: list indices must be integers or slices, not dict

What am I doing wrong?

@tirkarthi
Copy link
Contributor

I guess you should be passing data as a dictionary with field name and value.

from schematics.models import Model
from schematics.types import StringType, ListType, DictType


class LearningAims(Model):
    aims = ListType(DictType(StringType), required=True)


data = {"aims": [{"name": "foo"}, {"name": "bar"}]}

LearningAims(data).validate()

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

No branches or pull requests

2 participants