如何在 Django/PyAMF 和 Flex 之间映射字段名称?

发布于 2024-09-03 22:19:53 字数 939 浏览 4 评论 0原文

例如,使用我的 UserProfile 模型:

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 )
  ...

因此,我最终得到一个名为“public”的字段。这在 ActionScript 中并不适用,因为它是一个关键字。更改很烦人,因为它已融入 django-profile 包中的多个功能层中。因此,我被迫在 Flex 端重命名它:

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

在事务的任意一侧,我可以在哪里声明“远程字段 public 转换为本地字段 _public”?我在 PyAMF 方面对 ClassAliases 进行了一些混乱,但它很快就变得混乱,并且没有关于如何很好地做到这一点的文档。 Flex 方面的文档似乎表明有一个我可以覆盖的“处理传入请求”处理程序,但我认为它是在已经填充 com 对象中的字段之后发生的,因此将它们放在地板上,因为适当的字段不存在,给我留下了一堆:

ReferenceError: Error #1056: Cannot create property

在 Flex 跟踪中......

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 this 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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海蓝天 2024-09-10 22:19:53

为了支持这一点,PyAMF 需要提供字段之间的同义词映射。在那之前,您可以使用 IExternalizable (尽管笨拙) ):

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()

使用相应的 Flex 代码:

[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();
  }
}

注意我还没有测试上面的代码,但原则上应该可以工作。

顺便说一句,您能否更详细地了解文档中令人困惑的地方?我很想让新用户尽可能清楚地了解这一点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文