5 ms·
I've been doing a ton of work in both recently. In particular, i've been the lead dev on a new product for web and mobile in django, an API layer supporting an
by jspiral 14y ago
I've been doing a ton of work in both recently. In particular, i've been the lead dev on a new product for web and mobile in django, an API layer supporting an iphone app over an existing SQL server database in rails, and a rewrite of the backend for some kiosks in rails, on top of various contributions to other people's projects.
I want to make some comments on writing APIs in particular, since I've been doing a lot of it lately, as my projects almost always have several interfaces these days, e.g. native mobile apps, angular.js web frontends, and as I mentioned above, fat clients running on kiosk hardware.
Building json apis in rails is a pleasure. My mini stack inside of rails is controller -> request handler -> model layer + external services -> rabl. Very clean, few lines of code, easy to maintain, does exactly what you want.
Doing the same with default django is a little less obvious. Tastypie is cool but when you get much beyond providing CRUD over the web to your models, it gets messy. Forget nesting stuff more than one layer deep. In the end, I build a controller layer that matches the way rails works and it's just about as nice, but I have to know and follow the conventions myself.
In particular, i'm copying rails style validators for models, I've built something similar to my rails request handlers using django forms to handle input, and i've built a simplified controller layer that handles routing, paging, filtering, sorting, etc. Instead of rabl, I just write a to_dict function to package data into a response. not quite as slick, but roughly equivalent.
I had to do more on the django side to get there, but at this point both are equivalent for me in terms of building APIs.
I understand that these days there are compelling arguments for building APIs in neither of these languages. However, for speed of development, ease of also putting up a vanilla web interface, and richness of the ecosystem as a whole, I still like django and rails for building mvps.
- squidsoup 14y agoI have been enjoying working with Django Rest Framework (http://django-rest-framework.org/ http://django-rest-framework.org/) recently. It has a less magical approach than TastyPie and uses Django conventions like class-based views. My favourite feature is that the framework generates a web interface, making the API browseable (http://restframework.herokuapp.com/ http://restframework.herokuapp.com/).
- iends 14y agoI've used both tastypie and django-rest-framework for large projects and my preference is also django-rest-framework.
- jspiral 14y agoTo both the django-rest-framework advocates: thanks for the recommendation, will check it out. I got deep in the guts of tastypie and wasn't happy with the way it handles patching and nested objects in particular. For my kiosk project, as an example, I need to send a nested object graph over the wire. The wifi card in the kiosks isn't very reliable, so doing it in one call is very helpful (only one thing to retry a couple times, vs 10-ish objects, max depth of 4). From what I can tell, django-rest-framework is read-only beyond one level of depth (you can return a nested hierarchy, but not post it in one shot) Also, some of my objects are transient (e.g. credit card data), so I don't have a django model for each object that is an input to the api. (I have a plain old python class with some special magic for parsing track data, validation fields, etc for credit card and drivers license in particular, can't ever save that data to the db). would you use django-rest-framework for that? (also, is there a django equivalent of ActiveAttr, which lets you make ruby classes that act like active record objects without the persistence? or would you just make models and never save them? probably wouldn't look so good in an audit though)
- iends 14y agoI actually just had a conversation at work about this (although unrelated to Django). Almost all REST frameworks I've encountered have no support for POSTing of nested objects. I do not know of an ActiveAttr equivalent.