Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django-piston: How can I get app_label + model_name?

Before I was just using the build-in django serializers and it added a model field.

{
    pk: 1
    model: "zoo.cat"
}

How can I get the same model field using django-piston?

I tried fields = ('id', 'model') but that didn't work.

like image 622
Pickels Avatar asked Mar 15 '11 19:03

Pickels


Video Answer


2 Answers

Added this to my model:

def model(self):
    return "{0}.{1}".format(self._meta.app_label, self._meta.object_name).lower()

And this to my BaseHandler:

fields = ('id', 'model')

Seems to work. If anybody has other solutions feel free to post them.

like image 131
Pickels Avatar answered Oct 30 '22 19:10

Pickels


As your code for app_label:

    instance._meta.app_label

for model_name:

   instance.__class__.__name__

and with get_model can get model name from strings or url!

like image 23
Alireza Savand Avatar answered Oct 30 '22 18:10

Alireza Savand