flat json for foreignKeys

43 views
Skip to first unread message

jujule

unread,
Mar 17, 2011, 7:49:49 AM3/17/11
to django-piston
Hi all

i a new django-piston lover :)

how to make the foreignkey not nested in the JSONemiiter ?

i'd like to replace

"company": {
"_state": "<django.db.models.base.ModelState object at
0x897a82c>",
"id": 1,
"name": "sony"
},

with

"company":1



Dont know exactly where to make this... handler.read ? emitter ?

Thanks !!

Julien

jujule

unread,
Mar 17, 2011, 8:10:51 AM3/17/11
to django-piston
Maybe tweaking Emitter.contructor._fk but i dont know how to override
a function inside a function in Python.

Or maybe a better way to handle this ?

Thanks

Yohan Boniface

unread,
Mar 17, 2011, 8:35:17 AM3/17/11
to django...@googlegroups.com
Hi !

If I understand well, you can easyly do this with a handler classmethod :

https://bitbucket.org/jespern/django-piston/wiki/Documentation#!configuring-handlers

For example :

class FooHandler(BaseHandler):
fields = ('my_field', 'my_other_field', 'company')

@classmethod
def company(cls, item):
return item.company.pk
Something like this should work.


Yohan

2011/3/17 jujule <julien.b...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "django-piston" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-pisto...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-piston?hl=en.


jujule

unread,
Mar 17, 2011, 8:41:42 AM3/17/11
to django-piston
woot! works perfectly thanks :)

any way to have something more dynamic ?

@classmethod
def getFk(cls, item, field):
return getattr(item, field).pk

Thanks :)

Julien

On 17 mar, 13:35, Yohan Boniface <yohanbonif...@gmail.com> wrote:
> Hi !
>
> If I understand well, you can easyly do this with a handler classmethod :
>
> https://bitbucket.org/jespern/django-piston/wiki/Documentation#!confi...
>
> For example :
>
> class FooHandler(BaseHandler):
>    fields = ('my_field', 'my_other_field', 'company')
>
>    @classmethod
>    def company(cls, item):
>       return item.company.pk
>
> Something like this should work.
>
> Yohan
>
> 2011/3/17 jujule <julien.bouquil...@gmail.com>

Yohan Boniface

unread,
Mar 19, 2011, 2:09:10 PM3/19/11
to django...@googlegroups.com
Humm... I can't see a piston standard generic way to do this...
In my point of view, you'll have to override emitters if you really want to do this.
But maybe I'm wrong and I'm missing something...

Being a relatedfield is not a concept you can manage in Piston.
The default behavior for a related model, is to look for a handler to manage it.
And so this handler will manage the related model as a fields collection, not as one simple value.

Anyway, I'm not sure is "REST philosophy compliant" to consider a related model as a value, and not as a another resource...

Not sure I'm clear ;)

2011/3/17 jujule <julien.b...@gmail.com>
Reply all
Reply to author
Forward
0 new messages