gpt4 book ai didi

django - 如何使用 Flex 访问 Django 中的外键字段?

转载 作者:行者123 更新时间:2023-12-02 07:01:57 26 4
gpt4 key购买 nike

我有以下 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”结果为空:

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

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

最佳答案

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

型号:

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_lated() 填充,当 select 相关丢失时,evt._who_cache.lname 也会丢失。如果我摆脱 select_lated() 调用,那么我会看到错误:

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

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

关于django - 如何使用 Flex 访问 Django 中的外键字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/481110/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com