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

Does it support nested data on the fields declaration? #11

Closed
marcio0 opened this issue Mar 12, 2014 · 6 comments
Closed

Does it support nested data on the fields declaration? #11

marcio0 opened this issue Mar 12, 2014 · 6 comments

Comments

@marcio0
Copy link

marcio0 commented Mar 12, 2014

As a django-piston user, it is common to have nested data on the fields declaration:

fields = (
    'id', 'username',
    ('group', ('id', 'name'))
)

On django-tastypie, this is similar to use full = True on ForeignKey fields.

Is there a way to work with nested data?

@toastdriven
Copy link
Owner

As of right now, that would be a manual process (overriding prepare - https://github.com/toastdriven/restless/blob/master/restless/resources.py#L463-L490). Restless can currently traverse into a given object (looking up deep information), but doesn't really have a concept of returning nested data unless you explicitly hand that data back.

It'd be nice to have some kind of support for this in the future (so I'll leave the issue open), but for now, it's BYOD (bring your own data). For example, you could do:

class MyResource(DjangoResource):
    # Do **NOT** specify fields.
    # fields = {}

    def details(self, id):
        user = User.objects.get(pk=id).select_related('group')
        return {
            'id': user.id,
            'group': {
                'id': user.group.id,
                'name': user.group.name,
            }
        }

Not super-elegant (& there's definitely ways to abstract your own logic), but it would at least function for now.

@toastdriven
Copy link
Owner

This should be easier to implement as of Restless 2.0.0, at least in terms of abstraction & less pain. A custom FieldsPreparer subclass could override the prepare method (https://github.com/toastdriven/restless/blob/master/restless/preparers.py#L42-L60) & change behavior based on if the lookup is a dict or a string.

I'm still thinking about if I want to support this natively (maybe a NestedFieldsPreparer). Possibly in Restless 2.1.0.

@schmitch
Copy link
Contributor

It would be great if you would support this natively since i think that feature will be used very frequently!

@Morpho
Copy link

Morpho commented Sep 29, 2014

Would be a great feature. Right now I manually build the json dict as suggested in #11 (comment)

@seocam seocam self-assigned this May 22, 2017
@seocam
Copy link
Collaborator

seocam commented May 23, 2017

I'm working on that in the branch subpreparers (or something similar to that). Stay tuned!

@seocam
Copy link
Collaborator

seocam commented Jun 1, 2017

This was merged today. Restless 2.1 will be released in a bit! Thanks for the feature request @marcio0!

@seocam seocam closed this as completed Jun 1, 2017
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

5 participants