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

ListField does not respect value None (default value is []). #92

Open
kashoory opened this issue Sep 19, 2017 · 1 comment
Open

ListField does not respect value None (default value is []). #92

kashoory opened this issue Sep 19, 2017 · 1 comment

Comments

@kashoory
Copy link

Basically, there is no way to get rid of a ListField element in output json.
For other fields, you set them to None and it will be removed from output of to_struct(), but this does not apply to ListField. I reckon, ideally, None value should remove it from json and [] should put a [] in json.
It looks like the default value for ListField is [] so there is no way for to_struct() to understand whether user set the field to None, [] or left it unassigned.
Also, it looks like 'required' and 'nullable' properties are ignored for ListField.

@denisvolokh
Copy link

You can override to_struct method in your model, check all ListFields for len == 0 and skip it like it is doing for None values.

def to_struct(self):

    self.validate()

    resp = {}
    for _, name, field in self.iterate_with_name():
        value = field.__get__(self)
        _type = type(field)
        if value is None or (isinstance(field, fields.ListField) and len(value) == 0):
            continue

        value = field.to_struct(value)
        resp[name] = value

    return resp

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