开发者

How can I map field names between Django/PyAMF and Flex?

开发者 https://www.devze.com 2023-01-02 09:39 出处:网络
For example, using my UserProfile model: class UserProfile(models.Model): user= models.ForeignKey( User, unique=True )

For example, using my UserProfile model:

class UserProfile(models.Model):
  user      = models.ForeignKey( User, unique=True )
  blurb     = models.CharField( max_length=200, null=True, blank=True )
  public    = models.BooleanField( default=True )
  ...

Thus, I end up with a field called "public". This doesn't jive in ActionScript because it's a keyword. It's annoying to change because it's baked into several layers of functionality in the django-profile package. So, I'm forced to rename it on the Flex side:

[RemoteClass(alias="...")]
[Bindable]
public class UserProfile
{
    public function UserProfile()
    {
    }
    public var id:int;
    public var blurb:String;
    public var _public:Boolean;
    ...

Where, on either side of the transaction, can I state "remote field public translates to local field _public"? I messed around a bit with ClassAliases on the PyAMF side but it got messy quickly and there's no documentation on how to do t开发者_JAVA百科his nicely. And the documentation on the Flex side seems to indicate that there's a "process the incoming request" handler that I can override, but I think it occurs after already populating the fields in the com object, thus dropping them on the floor, since the appropriate field is not there, and leaving me with a bunch of:

ReferenceError: Error #1056: Cannot create property

in the Flex trace...


In order to support this, PyAMF needs to provide a synonym mapping between fields. Until then, you could use IExternalizable (although clumsily):

class UserProfile(model.Model):
  user      = models.ForeignKey( User, unique=True )
  blurb     = models.CharField( max_length=200, null=True, blank=True )
  public    = models.BooleanField( default=True )

  class __amf__:
    external = True

  def __writeamf__(self, output):
    output.writeObject(self.id)
    output.writeObject(self.blurb)
    output.writeObject(self.public)

  def __readamf__(self, input):
    self.id = input.readObject()
    self.blurb = input.readObject()
    self.public = input.readObject()

With the corresponding Flex code:

[RemoteClass(alias="...")]
[Bindable]
public class UserProfile implements IExternalizable
{
  public function UserProfile()
  {
  }
  public var id:int;
  public var blurb:String;
  public var _public:Boolean;

  public function writeExternal(output:IDataOutput)
  {
    output.writeObject(id);
    output.writeObject(blurb);
    output.writeObject(_public);
  }

  public function readExternal(input:IDataInput)
  {
    id = input.readObject();
    blurb = input.readObject();
    _public = input.readObject();
  }
}

Note I haven't tested the above code, but should work in principle.

Btw, can you go into greater detail about what was confusing about the documentation? I would love to make that as clear possible for new users.

0

精彩评论

暂无评论...
验证码 换一张
取 消