PyAMF / Django - Flex类映射错误

时间:2022-10-12 18:54:07

I'm using PyAmf to communicate with a Flex app. But I keep getting errors.

我正在使用PyAmf与Flex应用程序进行通信。但我不断收到错误。

My model:

from django.contrib.auth.models import User

class Talent(User):
    street = models.CharField(max_length=100)
    street_nr = models.CharField(max_length=100)
    postal_code = models.PositiveIntegerField()
    city = models.CharField(max_length=100)
    description = models.CharField(max_length=100)

My gateway file:

我的网关文件:

from pyamf.remoting.gateway.django import DjangoGateway
from addestino.bot.services import user
from addestino.bot.models import *
from django.contrib.auth.models import User
import pyamf

pyamf.register_class(User, 'django.contrib.auth.models.User')
pyamf.register_class(Talent, 'addestino.bot.models.Talent')

services = {
    'user.register': user.register,
    'user.login': user.login,
    'user.logout': user.logout,
}

gateway = DjangoGateway(services, expose_request=True)

The Flex Talent object:

Flex Talent对象:

package be.addestino.battleoftalents.model
{
    [Bindable]
    public class Investor
    {
    public static var ALIAS : String = 'be.addestino.battleoftalents.model.Investor';

    public var id:Object;
    public var street:String;
    public var street_nr:String;
    public var postal_code:uint;
    public var city:String;
    public var cash:Number;
    public var date_created:Date;
    public var date_modified:Date;
    public var username:String;
    public var password:String;
    public var email:String;

    public function Investor()
    {
    }      
}

}

If Flex calls my register servicemethod (a method that sends a flex Investor to python), I get an error 'KeyError: first_name'. Then when we add a first_name field to our Flex VO, we get a last_name error. And so on. This error means that our flex VO has to have exactly the same fields as our django models. With simple objects this wouldn't be a problem. But we use subclasses of the django User object. And that means our Investor also needs a user_ptr field for example. Note: I get all errors before the servicemethod.

如果Flex调用我的注册servicemethod(一个将flex Investor发送到python的方法),我收到一个错误'KeyError:first_name'。然后,当我们向Flex VO添加first_name字段时,我们会收到last_name错误。等等。此错误意味着我们的flex VO必须与我们的django模型具有完全相同的字段。对于简单的对象,这不是问题。但是我们使用django User对象的子类。这意味着我们的投资者还需要一个user_ptr字段。注意:我在servicemethod之前得到所有错误。

Is there an easier way? Ideally we would have a Flex Investor VO with only the fields that we use (whether they're from the Django User or our django Investor that extends from User). But right now the Flex objects would have to be modeled EXACTLY after our Django objects. I don't even know exactly what the Django User object looks like (which I shouldn't).

有没有更简单的方法?理想情况下,我们会有一个Flex Investor VO,只有我们使用的字段(无论是来自Django用户还是来自用户的django Investor)。但是现在我们的Django对象之后必须完全模拟Flex对象。我甚至不知道Django User对象的确切含义(我不应该这样)。

I could really use some help. Thanks a lot in advance :-)

我真的可以使用一些帮助。非常感谢提前:-)

2 个解决方案

#1


This is done using the IExternalizable interface.

这是使用IExternalizable接口完成的。

It lets you explicitly write and read objects. If this is similar to Java implicit serialization, it's not going to let you limit what is sent by default. I was unable to find any examples of this with PyAMF.

它允许您显式写入和读取对象。如果这与Java隐式序列化类似,则不会限制默认发送的内容。我无法用PyAMF找到任何这方面的例子。

Best post on Serialization I've found.

关于序列化的最佳帖子我发现了。

#2


With the release of PyAMF 0.6b2 I can finally answer this question.

随着PyAMF 0.6b2的发布,我终于可以回答这个问题了。

0.5.1 was pretty strict in how it handled inheritance when it came to encoding Django models. It ensured that all properties were guaranteed to be encoded on each object - and expected that all properties were available when decoding the request.

在编写Django模型时,0.5.1处理继承的方式非常严格。它确保所有属性都保证在每个对象上进行编码 - 并且期望在解码请求时所有属性都可用。

This sucked and the behaviour has now changed with the release of the new version. PyAMF is a lot more forgiving about what you hand it from Flex. You shouldn't get the KeyError error any more (and if you do, its considered a bug).

这很糟糕,随着新版本的发布,行为现在已经改变了。 PyAMF对你从Flex处理它的内容更加宽容。你不应该再得到KeyError错误(如果你这样做,它被认为是一个错误)。

#1


This is done using the IExternalizable interface.

这是使用IExternalizable接口完成的。

It lets you explicitly write and read objects. If this is similar to Java implicit serialization, it's not going to let you limit what is sent by default. I was unable to find any examples of this with PyAMF.

它允许您显式写入和读取对象。如果这与Java隐式序列化类似,则不会限制默认发送的内容。我无法用PyAMF找到任何这方面的例子。

Best post on Serialization I've found.

关于序列化的最佳帖子我发现了。

#2


With the release of PyAMF 0.6b2 I can finally answer this question.

随着PyAMF 0.6b2的发布,我终于可以回答这个问题了。

0.5.1 was pretty strict in how it handled inheritance when it came to encoding Django models. It ensured that all properties were guaranteed to be encoded on each object - and expected that all properties were available when decoding the request.

在编写Django模型时,0.5.1处理继承的方式非常严格。它确保所有属性都保证在每个对象上进行编码 - 并且期望在解码请求时所有属性都可用。

This sucked and the behaviour has now changed with the release of the new version. PyAMF is a lot more forgiving about what you hand it from Flex. You shouldn't get the KeyError error any more (and if you do, its considered a bug).

这很糟糕,随着新版本的发布,行为现在已经改变了。 PyAMF对你从Flex处理它的内容更加宽容。你不应该再得到KeyError错误(如果你这样做,它被认为是一个错误)。