add a field to model instance serialisation

70 views
Skip to first unread message

jujule

unread,
Mar 18, 2011, 8:54:25 PM3/18/11
to django-piston
Hi all

Im new to piston

Id like to add a 'unicode' field for any django model instance thats
get served by piston REST.

its easy to do if i add this to
https://github.com/django-piston/django-piston/blob/master/piston/emitters.py#L148

ret['__unicode__'] = str(data)

Problem is, this code reside in the Emitter.construct._model
subfunction which i cannot override properly except dirty monkey
patching

Any idea how to do this the good way ?

Thanks !

(NB : i want this field to appear also in related fields)

Yohan Boniface

unread,
Mar 19, 2011, 2:00:15 PM3/19/11
to django...@googlegroups.com
Hi Julien,

Tell me if I understand well :

for an instance called "obj" of a Model, where for example unicode(obj) == "Here is the unicode of my object", you want a field :
"__unicode__": "Here is the unicode of my object" ?

Yohan

2011/3/19 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 21, 2011, 2:58:26 AM3/21/11
to django-piston
Hello Yohan,

Yes that's it. For example i have theses 2 django models :

class Contact(models.Model):
first_name = models.CharFied(max_length=50)
last_name = models.CharFied(max_length=50)
def __unicode__(self):
return self.first_name


class Company(models.Model):
name = models.CharFied(max_length=50)
def __unicode__(self):
return self.name

Id like to have a [(id, __unicode__),...] list for any model through
piston.

Ive done it monkey patching Emitter.contruct._model but this is
dirty !

Anything cleaner or a way to 'extend' already defined emitters ?

Thanks

Julien

sorry for my poor english


On 19 mar, 19:00, Yohan Boniface <yohanbonif...@gmail.com> wrote:
> Hi Julien,
>
> Tell me if I understand well :
>
> for an instance called "obj" of a Model, where for example unicode(obj) ==
> "Here is the unicode of my object", you want a field :
> "__unicode__": "Here is the unicode of my object" ?
>
> Yohan
>
> 2011/3/19 jujule <julien.bouquil...@gmail.com>
>
>
>
>
>
>
>
> > Hi all
>
> > Im new to piston
>
> > Id like to add a 'unicode' field for any django model instance thats
> > get served by piston REST.
>
> > its easy to do if i add this to
>
> >https://github.com/django-piston/django-piston/blob/master/piston/emi...

Yohan Boniface

unread,
Mar 21, 2011, 4:51:27 AM3/21/11
to django...@googlegroups.com
Hi Julien,

Do not monkey patch so quickly ! ;)

Adding fields with Piston is really easy : just add it's name in one of the handler fields list.
fields = ("my_model_field", "my_extra_field")

"my_extra_field" can refer to :
- a local classmethod
- a handler's model related model
- a handler's model method
- a handler's model fields

So you can put what ever you want as field name in the list.
And if your extra_field is not a method or field of your original model, just add a local classmethod to do the job.
And of course use inheritence between handlers if you have the same needs for few handlers.

Anyway, if you just add "__unicode__" in the fields list, I should work as is, as __unicode__ is of course a method of your original model.
Never tried it, but I can't see a reason for this not to work.

Regarding the fields lists, in the piston native repo, you have two lists : fields and list_fields
The first list is used by the handler when serializing an instance, the second when serializing a list of instances (a queryset).

If you want to have different fields for a model when considered as a related model, you can have a look at our fork : https://bitbucket.org/liberation/django-piston/
We have added the related_fields possibility. And other few little things. For this, we hope the community fork will concretize soon ! :)

Yohan

PS : Are we speaking english here ? Really ? ;)


2011/3/21 jujule <julien.b...@gmail.com>

jujule

unread,
Mar 21, 2011, 5:07:19 AM3/21/11
to django-piston
Yohan, thanks for your help !

Ive first tried to add the __unicode__ field in the handler field
list. This works great but the __unicode__ field doesnt showup for
related objects.

For example, in my example, if i have a Contact.company =
models.ForeignKey(Compagny) , company wont have a __unicode__ field in
the JSON :

"data": [
{
"first_name": "Julien",
"last_name": "Boubou",
"company": {
"_state": "<django.db.models.base.ModelState object at
0x893ac8c>",
"id": 2,
"name": "CARREFOUR"
},
"birthdate": "2011-03-01",
"id": 1,
"__unicode__": "M Julien Bouquillonnne"
}
]

(I also have a secret '_state' key from i dunno where but i dont
mind.)

I just have a handler for 'Contact', not for 'Company', i guess this
is the problem. But i dont want to create a handler for any Django
model, just a generic one.

Having a __unicode__ field in any model would be useful for generating
combobox for related fields.

Im reading your fork, thanks !

Julien

On 21 mar, 09:51, Yohan Boniface <yohanbonif...@gmail.com> wrote:
> Hi Julien,
>
> 2011/3/21 jujule <julien.bouquil...@gmail.com>
Reply all
Reply to author
Forward
0 new messages