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

Add support of nested paths #12

Open
grundic opened this issue Aug 20, 2016 · 3 comments
Open

Add support of nested paths #12

grundic opened this issue Aug 20, 2016 · 3 comments

Comments

@grundic
Copy link
Contributor

grundic commented Aug 20, 2016

This is related to incorrectly created #7.
Can you add possibility to specify field disposition in case it's not at root level? Here is small example:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from simplemodels.fields import CharField, IntegerField
from simplemodels.models import Document


class User(Document):
    name = CharField()
    salary = IntegerField()


data = {
    'name': 'John Smith',
    'data': {
        'salary': 100500
    }
}


if __name__ == '__main__':
    user = User.create(data)
    print(user)

I would like to be able to specify in User model that the salary is nested under data path. And it should be possible to specify nested path. List of strings or usual uri should work fine:

class User(Document):
    name = CharField()
    salary = IntegerField(nested=['data'])

There one things to consider, though: should the configuration count the field itself, or only the nested path: ['data'] vs ['data', 'salary']. The latte less obvious, but let rename fields. But I would say that for that behaviour there should be another configuration option.

@prawn-cake
Copy link
Owner

prawn-cake commented Aug 20, 2016

Might be a good feature. I will ponder about design of this. No promises so far, but pull requests most welcome

UPD:
From my pov it should be a separate field type. It basically DictField field with custom getter + validation. To many specifics to extend current fields.

@grundic
Copy link
Contributor Author

grundic commented Aug 20, 2016

But what if the field is not a dictionary? It could be list of fields or other document, just nested deeper in the tree.
Right now it's possible to achieve the same behaviour using intermediate document:

from simplemodels.fields import CharField, ListField, DocumentField
from simplemodels.models import Document


class Comment(Document):
    body = CharField()


class User(Document):
    class _Data(Document):
        comments = ListField([Comment])

    name = CharField()
    data = DocumentField(_Data)


data = {
    'name': 'John Smith',
    'data': {
        'comments': [
            {'body': 'comment #1'},
            {'body': 'seconds comment'},
            {'body': 'one more comment'},
        ]
    }
}

if __name__ == '__main__':
    user = User.create(data)
    print(user.name)
    for comment in user.data.comments:
        print(comment)

But there other reasons, why I would prefer to have it described like this:

from simplemodels.fields import CharField, ListField, DocumentField
from simplemodels.models import Document


class Comment(Document):
    body = CharField()


class User(Document):    
    name = CharField()
    comments = ListField([Comment], nested=['data'])

grundic added a commit to grundic/simple-models that referenced this issue Aug 20, 2016
@prawn-cake
Copy link
Owner

ok, now I see your point

grundic added a commit to grundic/simple-models that referenced this issue Aug 21, 2016
grundic added a commit to grundic/simple-models that referenced this issue Aug 21, 2016
grundic added a commit to grundic/simple-models that referenced this issue Aug 21, 2016
Add support for DocumentField within nested path.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants