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

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, the author is a foreign key in my book. Now I would like to name the author when I call my BookVO in Flex. Thus, I would expect the code to work as follows, but "author_name" leads to null:

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

I understand that I can directly contact AuthorVO, but the essence of this question is how you can retrieve values ​​with a foreign key using Flex when your VOs are attached to a remote object? I am currently using PyAMF to bridge the gap between Flex and Django, but I'm not sure which is relevant.

+4
source share
2 answers

Ok, here is 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 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 is 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 is my ActionScript for the receiving side of an AMF call:

 protected function onRetrievedEvents(result: Object): void { for each(var evt: Object in result) { var who: Object = evt._who_cache.lname; ... 

The name evt._who_cache.lname is populated with select_related () and is absent when there is no associated selection. If I get rid of the select_related () call, I see an error:

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

You should try a different technique with your RemoteClass ... so select_related might not be a problem at all ... (otherwise my first answer would not be lost.) The rest is up to you.

+1
source

when you get your book from the database try using select_related ()

which is on this page down:
http://docs.djangoproject.com/en/dev/ref/models/querysets/

it, "automatically" will follow the foreign key relationships, selecting additional data from related objects when it completes its request. "This is a performance accelerator that leads to (sometimes large) large queries, but means key relationships will not require database queries."

I liked how transparent the access to the database is through PyAMF from Flex. It is really brilliant.

0
source

All Articles