Django on GAE: use_library vs Django-nonrel - django

I am really new to the world of App Engine development and I want to start with a test project on Django and GAE. I've done some research and found out that there are two major ways to use Django in my app on GAE.
Django-nonrel + djangoappengine
use_library() to load Django from SDK
Please tell about pros and cons of each way.
Is there anything better than django-nonrel if i will decide to distribute Django code with my project?

It's not a matter of two different ways to use Django - it's two different versions of Django. App Engine comes bundled with versions 0.96, 1.0 and 1.1 of Django, unmodified from the mainline release. Django-nonrel is a branch of Django, which adds support for App Engine for the database backend.
If you're writing a new app, and you want to use Django for the whole app, including the models, you should use Django-nonrel.

When using django-nonrel you can use all Django features (including admin, auth, models, queries). I don't have a complete list of django modules which are either not working or partly not working in app engine.
If you use the Django version via use_library you have to be careful with app engine's limitations (use their model api, their auth via google accounts and so on).

Related

Ways to run django on google app engine that uses ndb

I'm trying to shift the existing webapp2 and jinjia2 templating to Django. But the initial Django project that I wanted to shift that was also stated on Google website link has been deprecated hence I'm worried that there will be no more support in the future if I continue using it. What are the other Django project that I could use. One important criteria is that I could still use the ndb datastore with the new Django framework. I found two other alternative:
Django App Engine
and
Djangae.
May I know what are the difference between them or are there other alternatives out there?

What are the limitations of using Django nonrel with Google App Engine?

I understand that full django can be used out of the box with CloudSQL. But I'm interested in using HRD. I'd like to learn more about what percentage of django can be used with nonrel. Does middleware work? How about other features of the framework like i18n, forms, etc. Also does nonrel work with NDB?
The background here is that I've even using webapp2 and before that webapp and find them great until your project gets bigger. So for this project I'm interested to reevaluate other options.
The big limitation is that the datastore doesn't do JOINs, so anything that uses JOINS, like many-to-many relations won't work.
Any packages/middleware that uses many-to-many won't work, but others will.
For example, the sessions/auth middleware will work. But if you use permissions with auth, it won't. If you use the admin pages for auth, they use permissions, so you'll have some trouble with those too.
i8n works.
forms work.
nonrel does not work with ndb.
I don't know what you mean by "until your project gets bigger". django-nonrel won't help with the size of your app.
In my opinion there's two big reasons to use nonrel:
You're non-committal about App Engine. Nonrel potentially allows you to move to MongoDB as a backend.
You want to use django packages for "free". For example, I used tastypie for a REST API, and django-social-auth to get OAuth for FB/Twitter logins with very little effort. (On the flip side, with 1.7.0, they've addressed the REST API with endpoints)

How to migrate Django project to Google App Engine

I am looking for a guide to migrate Django project to Google App Engine and use Google's datastore. The most of the guides I found were linked to Django-Appengine using Django-nonrel (but I want to use GAE's native support).
Going through GAE getting started guide, it says:
Google App Engine supports any framework written in pure Python that speaks CGI (and any WSGI-compliant framework using a CGI adaptor), including Django, CherryPy, Pylons, web.py, and web2py. You can bundle a framework of your choosing with your application code by copying its code into your application directory.
I understand that I won't be able to use some features of Django in that case (majorly the admin feature) and would also need to restructure the models.
From other reading, I also found that latest SDK of GAE now includes Django 1.3 on Python 2.5.
I tried to put all files from my Django application to a GAE project, but couldn't get it all to work together.
Please provide some basic guide using which I may migrate my Django project to Google App Engine's code.
Thanks.
For an existing Django app, using django-nonrel is the simplest approach; it is very popular so you should be able to find help with specific errors you get quickly.
Another approach is written up in this article: http://code.google.com/appengine/articles/pure_django.html -- it goes the other way, taking an App Engine app that uses Django for dispatch, templates, and forms, but not for models, and describes how to make it run in a native Django environment. Maybe you can glean some useful hints for your situation from it.
I've used django-nonrel, which behaves pretty much like django, except that operations with JOINs will return errors. I've basically worked around this by avoiding ManyToMany fields, and essentially building that functionality manually with an intermediate table.
So far I've ran into two problems with Django-nonrel:
1. No access to ancestor queries, which can be run in a transaction. There's a pending pull request for this feature though.
2. You can't specify fields that are not indexed. This could significantly increase your write costs. I have an idea to fix this, but I haven't done so yet.
(Edit: You CAN specify fields that are not indexed, and I've verified this works well).
2 (new). Google is pushing a new database backend called ndb that does automatic caching and batching, which will not be available with django-nonrel.
If you decide not to use django-nonrel, the main differences are that Django models do not run under App Engine. You'll have to rewrite your models to inherit from App Engine's db.Model. Your forms that use Django's ModelForm will need to inherit from google.appengine.ext.db.djangoforms instead. Once you're on App Engine, you'd have to port back Django if you ever take your app somewher else.
If you already have a Django application you might want to check this out. You won't work with App Engine's datastore but Google Cloud SQL might fit your needs.

Django + Google App Engine: app engine helper for django or use_library?

There seem to be 2 ways to use django 1.1 with GAE
Google App Engine helper for django
The new use_library() function
We currently use the first. Should we switch? And what's the difference between the two?
use_library loads an unpatched version of django in the production environment, so many things will not work out of the box on app-engine.
The helper applies a series of patches to the django libraries to enable things like Sessions, test, cache framework, etc. If you do not add your own copy of django into your helper application and you are using the latest release (r100 or higher), the helper first tries to load django 1.1 and if it does not succeed then loads 1.0. You can see this in appengine_django/__init__.py::LoadDjango.
On production GAE, django 1.1 always exists, so it is loaded first.
However, on your development environment the dev server SDK does not distribute Django. Therefore, it uses whatever version of Django it can find, first trying 1.1 and then 1.0 and if it cannot find one then throws UnacceptableVersionError.
You probably want to use the helper and not use_library because then you will need to patch the raw django libraries yourself, thus duplicating the work in the helper. Whether you distribute your own version of django, either as a folder or zip file is up to you. One of the advantages of not distributing your own copy of django is that as google applies security patches you automatically get them without having to redeploy your application.
the replacement is called django-nonrel (and djangoappengine)... you can find it at
http://www.allbuttonspressed.com ... with django-nonrel, you should be able to run pure Django apps on top of App Engine without tweaking your models!
FYI, there is at least one more way to get Django 1.1 in GAE.
Take a look at http://code.google.com/p/app-engine-patch/
It allows use to use most of Django features including Admin.
app-engine-patch seems to have died:
http://code.google.com/p/app-engine-patch/issues/detail?id=253
As of GAE 1.5.0, there's a much simpler way of specifying a Django version.
In appengine_congif.py, include the line
webapp_django_version = '1.2'
This will cause the use_libary() to happen under the covers.

Django and App Engine

I wanted to check the status of running Django on the Google App Engine currently and what the benefits of running django on GAE over simply using Webapp.
Django main killer feature, IMHO, is the reuseable apps and middleware. Unfortunately, most current Django apps use models or model forms (django-tags, django-reviews, django-profiles, Pinax apps).
So what are the remaining features or benefits that django has that can still run in Google App Engine (other than what's disabled: the popular django apps, session and authentication middleware, users and admin, models, etc).
Also, is there a list of the Django apps that work in App Engine as well?
app-engine-patch currently has the most of django functional, including sessions, contrib.auth, sites, and some other standard django apps. However, its main drawback (my opinion) is that it uses a zip file of a modified version of django to achieve this functionality and the current maintainers don't seem to have kept pace with current django releases. Currently it seems to be the consensus of the past and present maintainers that this approach is too cumbersome to maintain and therefore no one is currently maintaining it.
google-app-engine-django, uses a monkey patch approach of the latest django version included in the production GAE runtime, so as long as google continues to track django releases you'll be kept up to date regarding django. However, it currently has not fully ported contrib.auth, so you can only authenticate with google accounts - which can be a big drawback depending on whether you want contrib.auth User models to work as you know them on sql backends. There is also no django admin support in the helper as there is in app-engine-patch. A fork of django-app-engine-django exists which adds in some of the contrib apps, such as flatpages, sites, and sitemaps. Also note, it only works on django versions up to 1.1, until issue #3230 Django 1.2 is added to use_library, unless you upload django as a zip file.
On the horizon, the original developer of app-engine-patch has been working on the django-nonrel branch, but this may be pretty far away from being included in a django release. This django developers thread has a lot of information about these efforts.
Separately, there is a google summer of code project working on integrating some aspects of nonrel db's.
app-engine-patch gets most of those things working inside AppEngine - so you can (mostly) use straight Modelforms, use the Django users and admin, etc.
I've only used it for fairly simple projects (being quite new to django), but they claim that most Django apps will work with (at most) minor modifications on appengine. For instance, app-engine-patch uses the AppEngine Model classes rather than the Django classes; and there are some of the basic views that are too inefficient to run on Appengine.
added: google-app-engine-django is similar; but provides a BaseModel that appears identical to Django's BaseModel. My understand is that google-app-engine-django was released by Google, then forked to create app-engine-patch. The maintainers of app-engine-patch seem to have some different goals from the creators of google-app-engine-django, so you may find that one of the two suits your needs better than the other.
Google have provided some articles on running Django apps on appengine; the most recent is actually a guest post from the authors of app-engine-patch.
I've had the best success by simply picking and choosing the Django features that I need and patching them into webapp myself. In my latest project I actually just cut out the webapp stuff entirely. I still import and call several webapp utility functions, but it is mostly a hand rolled application built from the good parts of GAE and Django.
You might be interested to check out web2py, another Python framework that supposedly has less friction between GAE and a "normal" web server.
It is now quite easy to use full Django on GAE:
https://developers.google.com/appengine/articles/django-nonrel#ps
The Django version provided with App Engine has been updated to 1.2.5 with the latest SDK release (1.4.2, changelog). This version is available through the use_library() declaration, so you no longer need to mess around with monkey patching to the same extent.
The GoogleAppEngine (GAE) Python 2.7 runtime provides several third-party libraries that your application can use, in addition to the Python standard library, GAE tools, and GAE Python runtime environment. One of them is Django. The below is copied from the GAE docs page on third-party libraries:
To use Django in Python 2.7, specify the WSGI application and Django library in app.yaml:
...
handlers:
- url: /.*
script: main.app # a WSGI application in the main module's global scope
libraries:
- name: django
version: "1.2"