« Return to Thread: How can I use Flex to access foreign-keyed fields in Django?

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

by huuuze@gmail.com :: Rate this Message:

Reply to Author | View in Thread

Nick...thank you for the quick response.

I've implemented your patch, but I'm still coming up empty.  When I view my BookVO, the "author" attribute is null (despite using "select_related()").  However, I will note that I can access the author name if I call "_author_cache.name".  Not sure if that's how you intended it to work.

bryan

On Mon, Jan 26, 2009 at 6:57 PM, Nick Joyce <nick@...> wrote:
Hi Bryan,

Welcome!

I hadn't realised that foreign key relationships were not being followed on encoding.

I have spent some time putting together an adapter that support django foreign keys out of the box without you (the user) having to do anything except provide a `select_related`.

An example:

# models

class Author(models.Model):
   name = models.CharField(max_length=100)

class Book(models.Model):
   title = models.CharField(max_length=50)
   author = models.ForeignKey(Author)

# views
import pyamf
from pyamf.remoting.gateway.django import DjangoGateway

from myproject.myapp.models import Book

def only_books():
   return Book.objects.all()

def books_and_authors():
   return Book.objects.select_related().all()

gateway = DjangoGateway({
   'only_books': only_books,
   'books_and_authors': books_and_authors
})

The `only_books` method will return a list of book objects with no author objects (as foreign keys are lazily loaded) whereas `books_and_authors` will return books with author objects attached to them. <-- Hope that all makes sense :)

If you want this behaviour then apply the diff attached to your pyamf tree. I have (lightly) tested for encoding but not decoding so use with caution.

If the patch seems reasonable we can get some tests for it and make it part of 0.4.1.

Joel, this should save you some time investigating the cache issue you were having - hope it goes some way to solving your problem.

Cheers,

Nick





On 26 Jan 2009, at 22:11, Bryan Hughes wrote:

I don't mean to second guess, but are you certain DjangoAMF doesn't handle ForeignKeys in the manner I described?  I read through this post (http://sourceforge.jp/forum/forum.php?forum_id=10573&thread_id=16153) and the author implies this functionality has been made available in DjangoAMF 0.7.

Thank you again for your help.  Just getting frustrated with something I was hoping would be straightforward.


On Mon, Jan 26, 2009 at 4:44 PM, Joel <imlowdown@...> wrote:
DjangoAMF doesn't handle it at all, you simply get the title_id:int back (as you do with PyAMF). It is also unsupported (for all practical purposes) and has some fairly crippling errors. Do not want.

Nick explained to me earlier this month how to override the Cache creation class to build your own custom cache. I would bet you could do this and attach the object cleanly to the returned object. I haven't tried this yet, but it is on my list of things to do. I will report back if I have success. Will probably test it out tonight.


On Mon, Jan 26, 2009 at 3:35 PM, Bryan Hughes <huuuze@...> wrote:
Thanks for the response.  From what I gather, adding the _cache fields to my VO seems a little dirty.

Does anyone know if DjangoAMF handles this dilemma in a cleaner manner?



On Mon, Jan 26, 2009 at 4:30 PM, Joel <imlowdown@...> wrote:
"I personally hate this and wish it was either cleaner or more controllable (in an easy way)."

I should add, but not so much that I get off my lazy butt and contribute to the project in a meaningful way. Thanks PyAmf dev team, you guys are awesome! I would like to turn off the caching globally though :>


On Mon, Jan 26, 2009 at 3:24 PM, Joel <imlowdown@...> wrote:
http://pastebin.com/m2ffcb77d

I use these methods to grab related models and whatnot.


ReferenceError: Error #1056: Cannot create property _title_cache on com.myproject.models.vo.BookVO

If you create a TitleVO object and then make a _title_cache:TitleVO property it will do some ORM bits. I personally hate this and wish it was either cleaner or more controllable (in an easy way).

-joel


On Mon, Jan 26, 2009 at 3:01 PM, Bryan Hughes <huuuze@...> wrote:
I need some help!  Please see the question I posted here:

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

Does anyone know of a solution?  The one posted in the thread results in the following error:

ReferenceError: Error #1056: Cannot create property _title_cache on com.myproject.models.vo.BookVO

_______________________________________________
PyAMF users mailing list - users@...
http://lists.pyamf.org/mailman/listinfo/users




_______________________________________________
PyAMF users mailing list - users@...
http://lists.pyamf.org/mailman/listinfo/users



_______________________________________________
PyAMF users mailing list - users@...
http://lists.pyamf.org/mailman/listinfo/users



_______________________________________________
PyAMF users mailing list - users@...
http://lists.pyamf.org/mailman/listinfo/users


_______________________________________________
PyAMF users mailing list - users@...
http://lists.pyamf.org/mailman/listinfo/users


_______________________________________________
PyAMF users mailing list - users@...
http://lists.pyamf.org/mailman/listinfo/users



_______________________________________________
PyAMF users mailing list - users@...
http://lists.pyamf.org/mailman/listinfo/users

 « Return to Thread: How can I use Flex to access foreign-keyed fields in Django?