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

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.

👤Jim Carroll

0đź‘Ť

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.

👤Jim Carroll

Leave a comment