简体   繁体   English

PyAMF / Django-Flex类映射错误

[英]PyAMF / Django - Flex class mapping errors

I'm using PyAmf to communicate with a Flex app. 我正在使用PyAmf与Flex应用程序进行通信。 But I keep getting errors. 但是我不断出错。

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' . 如果Flex调用我的register服务方法(一种将flex Investor发送到python的方法),则会收到错误消息'KeyError: first_name' Then when we add a first_name field to our Flex VO, we get a last_name error. 然后,当我们在Flex VO中添加first_name字段时,我们会收到last_name错误。 And so on. 等等。 This error means that our flex VO has to have exactly the same fields as our django models. 此错误意味着我们的flex VO必须具有与django模型完全相同的字段。 With simple objects this wouldn't be a problem. 对于简单的对象,这将不是问题。 But we use subclasses of the django User object. 但是,我们使用django User对象的子类。 And that means our Investor also needs a user_ptr field for example. 这意味着我们的投资者还需要例如一个user_ptr字段。 Note: I get all errors before the 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 ). 理想情况下,我们将只有一个使用我们使用的字段的Flex Investor VO(无论它们来自Django User还是我们的django Investor都来自User )。 But right now the Flex objects would have to be modeled EXACTLY after our Django objects. 但是现在,Flex对象必须完全按照我们的Django对象建模。 I don't even know exactly what the Django User object looks like (which I shouldn't). 我什至不知道Django User对象是什么样子(我不应该)。

I could really use some help. 我真的可以使用一些帮助。 Thanks a lot in advance :-) 在此先感谢:-)

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. 如果这类似于Java隐式序列化,则不会限制您默认发送的内容。 I was unable to find any examples of this with PyAMF. 我无法通过PyAMF找到任何示例。

Best post on Serialization I've found. 我发现了有关序列化的最佳文章

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. 在编码Django模型时,0.5.1在处理继承方面非常严格。 It ensured that all properties were guaranteed to be encoded on each object - and expected that all properties were available when decoding the request. 它确保所有属性都保证在每个对象上进行编码-并期望在解码请求时所有属性均可用。

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. PyAMF对您从Flex手中得到的东西宽容得多。 You shouldn't get the KeyError error any more (and if you do, its considered a bug). 您不应该再收到KeyError错误(如果确实如此,则将其视为错误)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM