如何使用 Flex 访问 Django 中的外键字段? [英] How can I use Flex to access foreign-keyed fields in Django?

查看:24
本文介绍了如何使用 Flex 访问 Django 中的外键字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 Django 和 Flex 代码:

I have the following Django and Flex code:

姜戈

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"结果为空:

As you can see in this example, Author is a foreign key in my Book model. Now I'd like to acccess the author's name when I call my BookVO in Flex. As such, I'd expect code like the following to work, but "author_name" results in a null:

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

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

I realize I could call an AuthorVO directly, but the crux of this question is how can you retrieve foreign-keyed values using Flex when your VOs are bound to a remote object? I'm currently using PyAMF to bridge the gap between Flex and Django, but I'm not sure that's relevant.

推荐答案

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

Ok, Here's an example...

型号:

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

Here's my 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:

and here's my Actionscript for the receiving side of the AMF call:

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() 调用,则会看到错误:

The evt._who_cache.lname is populated with the select_related() and missing when the select related is missing. If I get rid of the select_related() call, then I see the error:

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

你一定在用你的 RemoteClass 尝试不同的技术......所以 select_related 可能根本不是问题......(否则我的第一个答案不会被否定.)剩下的取决于你.

You must be trying a different technique with your RemoteClass... so the select_related might not be the problem at all... (otherwise my first answer wouldn't have gotten negged.) The rest is up to you.

这篇关于如何使用 Flex 访问 Django 中的外键字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆