How to use Django with GWT?

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

Do you know of any tool or library that could help me? Or maybe even the best way to achieve the same goal?

I found only these two: http://palantar.blogspot.com/2006/06/agad-tutorial-ish-sort-of-post.html Django + GWT or JQuery

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

+6
json django gwt
source share
5 answers

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

classname attribute1 type1 attribute2 type2 

Parsing with a little Python is very simple and therefore generates code for Django and GWT after you parse the lines above. Setting up for Django and GWT will take a bit more work. Accounting for foreign keys is more complicated. Don't forget the tiny little generated method for serializing objects, for example. in JSON format.

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

Finally, to speed up the development of GWT + Django, you can use the http servlet proxy in host mode, so you don’t need to collect your GWT stuff every time you want to start your GWT interface using the Django back end. The servlet at http://www.servletsuite.com/servlets/httpproxy.htm will probably do the trick.

+2
source share

In my Django / GWT project, I use django-piston to demonstrate Django models via the REST API in JSON format. Take a look at the piston website to learn more about how to achieve this http://bitbucket.org/jespern/django-piston/wiki/Home .

Then on the GWT side, you need to create overlay types for your Django models ( DevGuideCodingBasicsOverlay ).

I use gwt-dispatch ( http://code.google.com/p/gwt-dispatch/ ) with the addition of some REST ability to get my models from the Django backend, but you can use ( HttpRequestBuilder ), then use GWT JSONParser , and, having received JavaScriptObject and casting, you will get the desired model in GWT. It may seem overly complicated, but it is not, on the contrary, it is very convenient to use.

+6
source share

You do not have to manually create all the Django objects in JS. GWT consists only of user interface objects and interacts with Django's internal contour to display model data in a browser and send updates back. I used Django interfaces with Adobe Flex and GWT interfaces, and in no case did I have to re-create Django objects in JS. However, I used custom JSON encoders in my back-end. This was done so that I could send only the information necessary for the user interface, and not everything in the model instance (some of which may be confidential information).

+3
source share

It’s very difficult for me to get around repeating myself with two fancy frameworks like GWT and Django, which ultimately upset me so much that I refused it (I wrote the first article that you talked about).

Ultimately, what I ended up with was a switch to GWT / Java, where Java was the Google App Engine. Of course, there are compromises. It seems to me that Java is harder to configure, but easier when it will be. In the end, the DRY dictum was too big a siren to me. With GWT / GAE, your objects just go straight through and you don’t have to force your brain to switch between languages. Now you can learn about LiveCycle and Flex .;)

I hope this made sense and was useful - it was a long week! :)

PS, here is a new place for this GWT / Django Tutorial-ish Post

+1
source share

I completely agree with Vinay Sajip. If you are going to create an application with django with GWT, this means that django will be the backend, and GWT will be the interface, and they exchange data via JSON.

So, if you are thinking about exposing your basic django model directly for the GWT interface, your program is too simple or you are designing with a missing layer - data transfer objects (DTOs). When the GWT interface is present, it does not need all the attributes from the backend model, in fact, it should only transmit what it needs.

One simple example: if you are creating an email application, you certainly do not want to transfer all the content of the email body when downloading the email name listing. (Run firebug and spy on gmail, you will be delighted)

I also struggled with the same problem for a while, especially if you followed the RPC line of thinking.

0
source share

All Articles