tags:

views:

1369

answers:

4

So I know that I can communicate between those two using JSON, but I also know that I would have to manually recreate all Django objects in JS.

Do you know of any tool or library that could help me do that? Or maybe even a better way of achieving the same goal?

I only found these two: http://palantar.blogspot.com/2006/06/agad-tutorial-ish-sort-of-post.html http://stackoverflow.com/questions/990319/django-gwt-or-jquery

But then I still would have to manually mirror my objects, which would violate DRY.

+1  A: 

You don't necessarily have to manually create all Django objects in JS. The GWT consists of just the UI objects, and communicates with the Django back-end to get the model data to display in the browser, and to send updates back. I've used Django back-ends with both Adobe Flex and GWT front-ends, and in neither case did I have to re-create any Django objects in JS. I did, however, use customised JSON encoders in my back-end. This was done so that I could send only the information needed by the UI, rather than everything in a model instance (some of which might be sensitive information).

Vinay Sajip
+1  A: 

If you want to create a new model, both in GWT and in Django, the easiest way to generate code is to start from a very simple model definition, e.g.

classname
attribute1 type1
attribute2 type2

Parsing that with a little Python is very easy, and so is generating code for both Django and GWT once you parsed the above-like lines. Customization for both Django and GWT will take only a little more work. Taking foreign keys into account is more complicated. Don't forget a tiny little generated method to serialize the objects, e.g. in JSON format.

The first reflex I had was to use introspection in Python, but then I ran into trouble when introspecting foreign key fields in django models. Maybe I should take a look at the code of the django-docs project (available at google code).

Finally, to speed up your GWT+Django development, you can use a http proxy servlet in hosted mode, so that you don't need to compile your GWT stuff every time you want to run your GWT front end with a Django back end. The servlet on http://www.servletsuite.com/servlets/httpproxy.htm will probably do the trick.

lbp
+1  A: 

It's really hard to get around manually repeating yourself with two quirky frameworks like GWT and Django, something that eventually frustrated me so much that I gave it up (I wrote the first article that you referred to).

Eventually, what I ended up doing was switching over to doing GWT/Java where Java was the Google App Engine. There are trade-offs of course. The Java seems to me more difficult to set up, but easier once it is. In the end, the DRY dictum proved to be too much of a siren call to me. With GWT/GAE, your objects just pass straight through AND you don't need to force your brain to switch between languages. Now off to learn about LiveCycle and Flex. ;)

Hopefully that made sense and was helpful - it's been a long week! :)

-Palantar

P.S., here's the new location for that GWT/Django Tutorial-ish Post

Palantar
+3  A: 

In my Django/GWT project I do use django-piston to expose Django models via REST API, in JSON format. Look at piston's website for more on how to achieve that http://bitbucket.org/jespern/django-piston/wiki/Home.

Then, on GWT side, you only need to create overlay types for your Django models (http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsOverlay.html).

I use gwt-dispatch (http://code.google.com/p/gwt-dispatch/) with some REST goodness added to fetch my models from Django backend, but you can use you whatever you want (HttpRequestBuilder), then use GWT JSONParser, and by getting JavaScriptObject and casting, you will get your desired model in GWT. It may seems a little too complicated, but it's not, quite the opposite, it's very comfortable to use.

skrat