django - Can't exclude ForeignKey fields for User in Piston -
i have model:
# models.py django.contrib.auth.models import user class test(models.model): author = models.foreignkey(user, related_name="tests") title = models.charfield(_("title"), max_length=100)
then in api
folder django piston webservice:
class testhandler(basehandler): allowed_methods = ("get") model = test fields = ("title", ("author", ("username",))) def read(self, request, id): base = self.model.objects try: r = base.get(pk=id) return r except: return rc.not_found
if call webservice get:
{ "title": "a test" "author": { "username": "menda", "first_name": "", "last_name": "", "is_active": true, "is_superuser": true, "is_staff": true, "last_login": "2011-02-09 10:39:02", "password": "sha1$83f15$feb85449bdae1a55f3ad5b41a601dbdb35c844b7", "email": "b@a.as", "date_joined": "2011-02-02 10:49:48" }, }
i have tried use exclude
, doesn't work either.
how can username author
? thanks!
okay, problem piston using set of fields defined on user model handler class, rather nested fields specified here.
another user refers exact same issue on piston discussion group here:
http://groups.google.com/group/django-piston/browse_thread/thread/295de704615ee9bd
the problem apparently caused bug in piston's serialization code. in words of documentation:
by using model in handler, piston remember fields/exclude directives , use them in other handlers return objects of type (unless overridden.)
which good, except "(unless overridden.)" case doesn't seem handled correctly.
i think slight modification in emitters.py might fix issue (lines 160-193)...
if handler: fields = getattr(handler, 'fields') if not fields or hasattr(handler, 'fields'): ...dostuff... else: get_fields = set(fields)
which should (perhaps?) read
if fields: get_fields = set(fields) else: if handler: fields = getattr(handler, 'fields') ...dostuff...
if decide try patching emitters.py let me know if trick - it'd patched in django-piston.
cheers!
Comments
Post a Comment