Hello All,
I have been trying to use amfast with django for a couple of days now
and I have (somewhat) successfully passed objects from my DB to my
Flex client but have not succeeded in doing the reverse operation .
I was starting with a complex set of models so i decided to simplify
in order to understand where am stuck.
Note: I am new to python and django so be kind if you see some
ridiculous bit of code that makes no sense ;)
Here is my models.py without ManyToManyFields
///////////////////////////////////////////////
from django.db import models
from django.contrib.auth.models import User
class Equipment(models.Model):
name = models.CharField('Name',max_length=100)
def __init__(self, *args, **kwargs):
super(Equipment, self).__init__(*args, **kwargs)
def __unicode__(self):
return
self.name
class Character(models.Model):
name = models.CharField('Name',max_length=100)
def __init__(self, *args, **kwargs):
super(Character, self).__init__(*args, **kwargs)
def __unicode__(self):
return
self.name
///////////////////////////////////////////////////////////////////////////
My django_channels.py
//////////////////////////////////////////////////////////////////////////
import sys
import os
import utils
from amfast import class_def
from amfast.remoting.channel import ChannelSet
from amfast.remoting.django_channel import DjangoChannel
from models import *
channel_set = ChannelSet()
rpc_channel = DjangoChannel('amf')
channel_set.mapChannel(rpc_channel)
utils.setup_channel_set(channel_set)
class_mapper = class_def.ClassDefMapper()
equipment_def = class_def.ClassDef(Equipment, 'vos.Equipment',
('name'))
class_mapper.mapClass(equipment_def)
character_def = class_def.ClassDef(Character, 'vos.Character',
('name'))
class_mapper.mapClass(character_def)
# Assign mapper to endpoints
channel = channel_set.getChannel('amf')
channel.endpoint.encoder.class_def_mapper = class_mapper
channel.endpoint.decoder.class_def_mapper = class_mapper
//////////////////////////////////////////
And here is my controller.py
//////////////////////////////////////////
from models import *
from amfast.remoting.channel import SecurityError
class Controller(object):
def checkCredentials(self, user, password):
u = User.objects.get(username__exact=user)
if u:
if u.check_password(password):
return True
else:
raise SecurityError('Invalid credentials.')
else:
raise SecurityError('Invalid credentials.')
class CharacterController(object):
def getAllRecords(self):
results = list(Character.objects.all())
return results
def getRecordById(self,value):
result = Character.objects.get(pk=value)
return result
def getByPlayer(self,value):
results =
list(Character.objects.select_related().filter(name=value))
return results
def getEquipments(self,value):
char = value;
results = Character.objects.get(pk=
char.id).equipments.all()
return value
def saveRecord(self,value):
item = value
item.save()
return item
def deleteRecord(self,value):
item = value
item.delete()
return item
////////////////////////////////////////////////////////////
So in Flex when i try to create a Character object
c = new Character()
c.name="foo"
characterModel.save(c) // calls the remote object's save method which
maps to saveRecord in the py controller
i get the following error stack:
# ---- EXCEPTION DESCRIPTION BEGIN ---- #
# ---- Type ---- #
AttributeError
# ---- Detail ---- #
'Character' object has no attribute '_state'
# ---- Traceback ---- #
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/site-packages/AmFast-0.5.3_r535-py2.7-macosx-10.5-fat3.egg/
amfast/remoting/__init__.py", line 221, in invoke
self.body[0].invoke(request, self)
-
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/site-packages/AmFast-0.5.3_r535-py2.7-macosx-10.5-fat3.egg/
amfast/remoting/flex_messages.py", line 291, in invoke
msg.response_msg.body.body = target.invoke(packet, msg, self.body)
-
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/site-packages/AmFast-0.5.3_r535-py2.7-macosx-10.5-fat3.egg/
amfast/remoting/__init__.py", line 143, in invoke
return self.callable(*args)
-
File "/Users/bounce/Documents/_DEVELOPER/PYTHON/Django/amfM2M/../
amfM2M/amfm2m/controller.py", line 35, in saveRecord
item.save()
-
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/site-packages/django/db/models/base.py", line 434, in save
self.save_base(using=using, force_insert=force_insert,
force_update=force_update)
-
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/site-packages/django/db/models/base.py", line 446, in
save_base
using = using or router.db_for_write(self.__class__,
instance=self)
-
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/site-packages/django/db/utils.py", line 133, in _route_db
return hints['instance']._state.db or DEFAULT_DB_ALIAS
# ---- EXCEPTION DESCRIPTION END ---- #
[19/Nov/2010 05:53:59] "POST /amf HTTP/1.1" 200 500
I don't understand the 'Character' object has no attribute '_state' ??
I would appreciate any help you guys can provide !
Cheers,
B.