【问题标题】:How can I use Flex to access foreign-keyed fields in Django?如何使用 Flex 访问 Django 中的外键字段?
【发布时间】:2009-01-26 19:56:07
【问题描述】:

我有以下 Django 和 Flex 代码:

Django

class Author(models.Model):
  name = models.CharField(max_length=30)

class Book(models.Model):
  title = models.CharField(max_length=30)
  author = models.ForeignKeyField(Author)

弹性

package com.myproject.models.vo
{
    [Bindable]
    [RemoteClass(alias="myproject.models.Book")]

    public class BookVO
    {
        public var id:int;
        public var title:String;
        public var author: AuthorVO;
    }
}

正如您在此示例中所见,Author 是我的 Book 模型中的外键。现在,当我在 Flex 中调用 BookVO 时,我想访问作者的姓名。因此,我希望像下面这样的代码可以工作,但“author_name”会导致 null:

var book = new BookVO();
var author_name = book.author.name;

我意识到我可以直接调用 AuthorVO,但这个问题的关键是,当您的 VO 绑定到远程对象时,如何使用 Flex 检索外键值?我目前正在使用 PyAMF 来弥补 Flex 和 Django 之间的差距,但我不确定这是否相关。

【问题讨论】:

    标签: django apache-flex pyamf


    【解决方案1】:

    好的,这是一个例子......

    型号:

    class Logger(models.Model):
        lname = models.CharField(max_length=80)
    
        def __unicode__(self):
            return self.lname
        #
    #
    
    class DataSource(models.Model):
        dsname = models.CharField(max_length=80)
        def __unicode__(self):
            return self.dsname
        #
    #
    
    class LoggedEvent(models.Model):
        # who's data is this?
        who = models.ForeignKey(Logger)
        # what source?
        source = models.ForeignKey(DataSource)
        # the day (and, for some events also the time)
        when = models.DateTimeField()
        # the textual description of the event, often the raw data
        what = models.CharField(max_length=200)
        # from -1.0 to 1.0 this is the relative
        # importance of the event
        weight = models.FloatField()
    
        def __unicode__(self):
            return u"%2.2f %s:%s - %s" % (self.weight, self.source, self.who, self.what)
        #
    #
    

    这是我的 amfgateway.py

    def fetch_events(request, source):
        events = LoggedEvent.objects.select_related().all()
        return events
    #
    
    services = {
        'recall.fetch_events': fetch_events,
    }
    
    gateway = DjangoGateway(services)
    

    这是我的 AMF 调用接收方的 Actionscript:

    protected function onRetrievedEvents(result: Object): void {
    
        for each(var evt: Object in result) {
            var who: Object = evt._who_cache.lname;
    
    ...
    

    evt._who_cache.lname 由 select_related() 填充,当 select 相关缺失时缺失。如果我摆脱了 select_related() 调用,那么我会看到错误:

    TypeError: Error #1010: A term is undefined and has no properties.
    

    您必须在 RemoteClass 上尝试不同的技术...所以 select_related 可能根本不是问题...(否则我的第一个答案不会被否定。)其余的取决于您。

    【讨论】:

    • 吉姆,感谢您的详细回复。最终,我联系了 PyAMF 的首席开发人员,他发现了这个工作原理的一个错误。因此,他做出了将包含在 PyAMF 0.4.1 中的更改。净收益是您将不再需要调用“_cache”属性。
    • 太棒了! _cache 的东西没了就好了。
    【解决方案2】:

    当您从数据库中获取图书时,请尝试使用 select_related()

    在此页面下方:
    http://docs.djangoproject.com/en/dev/ref/models/querysets/

    它,“将自动“遵循”外键关系,在执行查询时选择额外的相关对象数据。这是一种性能提升器,会导致(有时很多)更大的查询,但意味着以后使用外键-关键关系不需要数据库查询。”

    我一直喜欢通过 Flex 的 PyAMF 无缝访问数据库。真是太棒了。

    【讨论】:

    • 一些进展。我收到以下错误。有什么想法吗? ReferenceError:错误 #1056:无法在 com.myproject.models.vo.BookVO 上创建属性 _title_cache。
    • hmmm...我不确定,但接下来我会尝试重置和同步数据库...(您当前的数据将被删除。)
    • 我会试一试,但我仍然持怀疑态度。只是出于好奇,这是你的工作吗?可以肯定的是,您可以访问外键模型的字段,对吗?如果是这样,您介意在 dpaste.com 或其他网站上发布示例吗?
    • 对不起,我一定没有理解这个问题......例如 Book.objects.select_related(depth=2).get(id=5) 将遵循两个级别的外键,并且您将获得 AMF 传回的所有数据。如果这不能解决问题,那么我错过了这个问题。
    • 顺便说一句...我认为您不需要在 Actionscript 端使用 models.vo.Book ... PyAMF 的接收端只会将对象作为具有子对象的对象获取各个领域。
    猜你喜欢
    • 2012-08-23
    • 2020-01-26
    • 2013-06-08
    • 2015-04-25
    • 1970-01-01
    • 2018-03-28
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多