Setting up Django and PyAMF to use Python models objects in Flex using RemoteObject

This post is only about setting up and installing Django and PyAMF. And a simple example of  Flex Client using RemoteObject.

Click here to know about and download Django and PyAMF. Prerequisites are Python 2.5 or 2.6. 3.0 is not yet supported by both of them.

Download and extract Django to a Directory. cd to it.

python install

To setup projects and apps, read this,

Download and extract pyAMF to a dir and cd to it.

python install

copy the ‘pyAMF’ directory which is under ‘build/lib.win32-2.6’ directory inside your extracted PyAMF folder. Paste it under $PYTHONHOME\Lib\site-packages.

I have created a Project and App and dir structure looks like this.


I have written a simple model and a method getAllItems inside Here is the code.

from django.db import models

def getAllItems(self):
    rows = Employees.objects.all()
    return rows

class Employees(models.Model):
    emp_no = models.IntegerField(primary_key=True)
    birth_date = models.DateField()
    first_name = models.CharField(max_length=42)
    last_name = models.CharField(max_length=48)
    gender = models.CharField(max_length=3)
    hire_date = models.DateField()
    class Meta:
        db_table = u'employees'

Now if we want to invoke getAllItems from Flex, we need to add a gateway file . Here is my under empl app.

from pyamf.remoting.gateway.django import DjangoGateway

import proj.empl.models as models

services = {
    'Employees': models
    # could include other functions as well

echoGateway = DjangoGateway(services)
#UPDATE: Pass expose_request=false parameter to DjangoGateway to skip http request object as a mandated parameter.

Here, note that inside services, ‘Employees’ is going to be your ‘destination’ of RemoteObject in Flex.
Update your URLConf like this. I am re-directing URL anything ending with gateway/ to my

from django.conf.urls.defaults import *
from proj.empl import gateway

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',

    ('^gateway/$', gateway.echoGateway)

We have successfully configured our Django/PyAMF. Let us write a simple Flex client to invoke getAllItems(). Our Remote Class here would be models with destination as ‘Employees’. I am using Flex 4 beta here.

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx=""
 xmlns:mx="library://" minWidth="1024" minHeight="768" initialize="application1_initializeHandler(event)">
 import mx.rpc.http.Operation;
 import mx.controls.Alert;
 protected function test_resultHandler(event:ResultEvent):void
 dg.dataProvider = event.result;

 protected function test_faultHandler(event:FaultEvent):void

 protected function application1_initializeHandler(event:FlexEvent):void
 <mx:RemoteObject id="test" endpoint="http://localhost:8000/gateway/"
 destination="Employees" result="test_resultHandler(event)" fault="test_faultHandler(event)">

 <mx:DataGrid id="dg"/>


Start your Django development server from inside Django project.

python runserver


Run the application and check your data in DataGrid:)

After running my Flex client

After running my Flex client

This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

2 Responses to Setting up Django and PyAMF to use Python models objects in Flex using RemoteObject

  1. Sun Jain says:

    The Scripting is very nicely explained!!

  2. Donna Bates says:

    Thanks for this blog. I am new at django and this will be a big help.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s