wlanslovenija / django-tastypie-mongoengine

MongoEngine support for django-tastypie.
Other
73 stars 59 forks source link

Question about accessing a referencefield from tastypie #55

Closed Seraf closed 11 years ago

Seraf commented 11 years ago

Hello,

First here is the code : API resources : https://github.com/Seraf/LISA/blob/6cf8d2ea7928c91231a30ef0734da81ac8d8fdbe/LISA-Engine/web/interface/api.py Models used :

These models have a referencefield to a user (as a user have a Workspace) I'm trying to create a Workspace resource by post method with Tastypie (using the swagger ui). It shouldn't be too complicated as I have only a simple model with a : user = ReferenceField(User, required=True)

The problem is I can't pass this reference from tastypie :

Using the id of my user object : 
{
  "user": "51cade8246e1fa47c14ddfcc",
  "name": "Dashboard"
}

Result in :
{
  "error": [
    "ValidationError (Workspace:None) (A ReferenceField only accepts DBRef or documents: ['user'])"
  ]
}

When I use the resource_uri : 
{
  "user": "/api/v1/user/51cade8246e1fa47c14ddfcc/",
  "name": "Dashboard"
}

Same problem : 
{
  "error": [
    "ValidationError (Workspace:None) (A ReferenceField only accepts DBRef or documents: ['user'])"
  ]
}

I don't know if it's related to mongoengine, tastypie, or the both .. So sorry, I post my issue here. Hope you will have some free time to help me. Thanks a lot for tastypie-mongoengine !

mitar commented 11 years ago

Where in you API have you defined reference fields for django-tastypie-mongoengine? Reference fields are not (cannot be) automatically mapped.

Seraf commented 11 years ago

Oh thanks, I tried that before but didn't get it to work. Now I have a partial result : With :

class WorkspaceResource(mongoresources.MongoEngineResource):
    user = fields.ReferenceField(to='web.lisa.api.UserResource', attribute='id', full=True)

It results with :

{
  "error": [
    "ValidationError (Workspace:lisa) (Invalid Object ID: ['id'] Field is required: ['user'])"
  ]
}

Do you have an idea how could I map the user id please ? My UserResource just map the mongoengine User object.

Sorry to annoy you with a stupid question like this, I understood it isn't tastypie-mongoengine related issue, but I think it lacks a little documentation around the user object.

Thanks a lot for your time !

mitar commented 11 years ago

I don't understand what you would like to map? If you have in your workspace document attribute "id", then on your API resource you also should have field "id". You just have to specify it manually, because there is no way that the system would know which is the related resource.

So what do you believe this line:

user = fields.ReferenceField(to='web.lisa.api.UserResource', attribute='id', full=True)

does?

mitar commented 11 years ago

BTW, you can always check tests for more examples as well.

Seraf commented 11 years ago

Hello, thanks for your tips. I fixed it with this : https://github.com/Seraf/LISA/commit/bd644cae7fd6f454779d668c8e9c3406d54ac6f8 I tried this by looking your unit tests. Don't know if it's the right way to go, but it works.

Thanks a lot !

mitar commented 11 years ago

Great.