How can I use Flex to access foreign-keyed fields in Django?

╄→гoц情女王★ 提交于 2019-12-06 04:10:26

问题


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.


回答1:


Ok, Here's an example...

Model:

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)
    #
#

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)

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;

...

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.

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.




回答2:


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.



来源:https://stackoverflow.com/questions/481110/how-can-i-use-flex-to-access-foreign-keyed-fields-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!