Skip to main content
 首页 » 编程设计

python之PyAMF、Django 和 Python 的 "property"功能问题

2025年05月04日37myhome

到目前为止,我已经成功地使用 PyAMF 在我的 Flex 前端和我的 Django 后端之间进行通信。但是,我相信我遇到了一个错误。以下示例(强调“示例”一词)演示了(潜在的)错误:

我的 Flex 应用程序包含以下 VO:

package myproject.model.vo 
{ 
    [Bindable] 
    [RemoteClass(alias="myproject.models.Book")] 
 
    public class BookVO 
    { 
        public var id:int; 
        public var title:String; 
        public var numberOfOddPages:int; 
    } 
} 

我的 Django 应用包含以下模型:

class Book(models.Models): 
    title = models.CharField(max_length=20) 
 
    def _get_number_of_odd_pages(self): 
        #some code that calculates odd pages 
       return odd_page_total 
 
    numberOfOddPages = property(_get_number_of_odd_pages) 

当我尝试检索要在 DataGrid 中显示的图书对象时,图书会按预期显示在网格中。但是,“numberOfOddPages”始终设置为 0。我什至尝试用默认值(即“numberOfOddPages=100”)显式设置此属性,以查看我的“_get_number_of_odd_pages()”方法是否有错误。不幸的是,它产生了相同的结果:VO 中的值仍然为 0。

有没有人知道我可能做错了什么?

请您参考如下方法:

我刚刚收到 PyAMF 的首席开发人员的以下回复。这绝对是一个错误:

This is a bug in the way the Django adapter handles non models.fields.* properties.

If I do:

import pyamf 
 
class Book(object):     
def _get_number_of_odd_pages(self): 
  return 52 
 
numberOfOddPages = property(_get_number_of_odd_pages) 
 
pyamf.register_class(Book, 'Book') 
 
encoded = pyamf.encode(Book()).getvalue()  
print pyamf.decode(encoded).next().numberOfOddPages 

Then i get the correct values of 52.

I have created a ticket for this and will look into getting a patch a little later.

Cheers,

Nick

更新:Nick 已经修复了这个错误,它将在 PyAMF 0.4.1 中发布(应该在本周末发布)。