如何使用Flex访问Django中的外键字段? [英] How can I use Flex to access foreign-keyed fields in Django?
问题描述
我有以下Django和Flex代码:
I have the following Django and Flex code:
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)
Flex
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;
}
}
正如你在这个例子中看到的,作者是外键我的书模型。现在我想在Flex中调用我的BookVO时收到作者的名字。因此,我希望以下代码可以正常工作,但是author_name会导致一个null:
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 ,这是一个例子...
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_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屋!