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

GET request response with Foreign Key Property(Name, etc.) instead of key #28

Open
get2abhi opened this issue Mar 4, 2016 · 2 comments

Comments

@get2abhi
Copy link

get2abhi commented Mar 4, 2016

I have the following models

class Plant(ndb.Model):
    name = ndb.StringProperty()
    company = ndb.KeyProperty(kind='Company')
    owner = ndb.KeyProperty(kind='User')
    class RESTMeta:
        user_owner_property = 'owner'
        include_output_properties = ['name']

class Department(ndb.Model):
    name = ndb.StringProperty()
    plant = ndb.KeyProperty(kind='Plant')
    owner = ndb.KeyProperty(kind='User')
    class RESTMeta:
        user_owner_property = 'owner'
        include_output_properties = ['name']

Now in the GET request for department is working fine and the output is

/api/department

{
  "next_results_url": null,
  "results": [
    {
      "owner": null,
      "plant": "aghzfnBnLWFwaXISCxIFUGxhbnQYgICAgIDyiAkM",
      "name": "Packing",
      "id": "aghzfnBnLWFwaXIXCxIKRGVwYXJ0bWVudBiAgICAmc6UCQw"
    }
  ]
}

But how can i get the plant name instead of its Key

@get2abhi get2abhi changed the title GET request output with Foreign Key Property(Name, etc.) GET request response with Foreign Key Property(Name, etc.) instead of key Mar 4, 2016
@budowski
Copy link
Owner

budowski commented Mar 6, 2016

The issue here is that we need to recursively decode the referenced object (plant in your case), but that's not always what the user wants. It's also problematic in case we have an endless recursion (e.g. two models that point to each other).

So we have 3 possible solutions at the moment:

  1. Use StructuredProperty (in your case, plant = ndb.StructuredProperty(Plant)). But that means that the Plant instance won't exist directly in the DB with its own key/ID (meaning, you won't be able to query for it directly).
  2. Do a second API call with the returned key for Plant (call GET /api/plant/<returned_key>).
  3. We implement in rest_gae recursive decoding of objects. However, we must make sure that - A) We won't do endless recursion of decoding models. B) We'll only decode models that have a specific flag turned on (e.g. using RESTMeta).

@get2abhi
Copy link
Author

get2abhi commented Mar 6, 2016

The 3rd option looks promising provided we take care of circular dependency in models.

Is it work in progress ?

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