3 ms·
Django does this out of the box: https://docs.djangoproject.com/en/1.7/topics/serialization/ https://docs.djangoproject.com/en/1.7/topics/serialization/ But DR
by andybak 12y ago
Django does this out of the box: https://docs.djangoproject.com/en/1.7/topics/serialization/ https://docs.djangoproject.com/en/1.7/topics/serialization/
But DRF adds lots of extra functionality. Even with DRF it's as simple as adding:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
to get full automatic serialization of a model.
> I don't know if serializing child objects is really a good idea
Depends on the specific case. For internal APIs I tend to create endpoints that give a view what it needs in a single request.
- norbert_mate 12y agoCorrect. It's very easy to create a simple model serializer and this is what the general model serializer does. It's no big deal just saves a few line of code.
- whalesalad 12y agoYou say "out of the box" but it's not that simple... 'blah blah DateTime is not JSON serializable'
- andybak 12y agoJust tried it: from django.core import serializers print serializers.serialize("json", StockCount.objects.all()[:2]) >> [{"fields": {"timestamp": "1900-01-02T00:00:00Z", "version": 0, "item_instance": null, "item_model": 408, "stock_level": 999, "location": 2}, "model": "stocklevels.stockcount", "pk": 1}, {"fields": {"timestamp": "1900-01-02T00:00:00Z", "version": 0, "item_instance": null, "item_model": 409, "stock_level": 999, "location": 2}, "model": "stocklevels.stockcount", "pk": 2}]