limodou wrote:
> django also uses simplejson to dump python variable, why you want to
> avoid it? And I think using simplejson is more flexiable and simple.
Because it's more generic and I don't want to recreate what's done in
the models...
In the end I did it like this:
# MODEL --------------------------------------------
class Book():
hidden_field = ...
user = foreign key
def api_additional(self)
return ({"username": user.username}, ["hidden_field"])
# VIEW --------------------------------------------
def api(request, id, model):
serializer = CustomJSONSerializer()
value = serializer.serialize(model.objects.filter(id=id))
return HttpResponse(value, mimetype='application/javascript')
# CustomJSONSerializer --------------------------------------------
class CustomJSONSerializer(JSONSerializer):
def end_object(self, obj):
super(JSONSerializer, self).end_object(obj)
fields = self.objects[-1]["fields"]
try:
additional_fields, removed_fields = obj.api_additional()
# remove the unwanted fields
for field in removed_fields:
if field in fields.keys():
del fields[field]
# add the additional fields
fields.update(additional_fields)
except AttributeError:
pass
# URLS --------------------------------------------
urlpatterns = patterns('bookshop.api.views',
(r'^models/book/(?P<id>\d+)/$', 'api', {"model": Book}),
Thus getting almost-for-free serialization with hidden fiends and
additional data :-) Perhaps something like this (but a bit smarter)
should go into the Serialization?
- bram
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---