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;
}
}
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;
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.
when you get your book from the database, try using select_related()
which is way down on this page:
http://docs.djangoproject.com/en/dev/ref/models/querysets/
it, "will automatically "follow" foreign-key relationships, selecting that additional related-object data when it executes its query. This is a performance booster which results in (sometimes much) larger queries but means later use of foreign-key relationships won't require database queries."
I've been loving how seamless the access to the database is through PyAMF from Flex. It's really brilliant.
Ok, Here's an example...
Model:
Here's my amfgateway.py
and here's my Actionscript for the receiving side of the AMF call:
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:
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.