as yejun wrote up at the top, the following works, but doesn't verify
that assignments to Book().first are instances of Page.

class Book(db.Model):
        title = db.StringProperty()
        first = db.ReferenceProperty()  # note - no reference_class is
specified


class Page(db.Model):
        text = db.TextProperty()
        next = db.SelfReferenceProperty()
        book = db.ReferenceProperty(Book)

I don't understand very much about __metaclass__ processing, so there
may be some way to define a metaclass for Book (probably involving a
modified version of db.ReferenceProperty) that does provide this
checking.  However, I'd be surprised if there was.

On Oct 15, 7:18 pm, Dado <[EMAIL PROTECTED]> wrote:
> Does that mean the aforementioned problem can't be solved?
>
> On Oct 15, 12:33 pm, Andy Freeman <[EMAIL PROTECTED]> wrote:
>
>
>
> > The problem really has nothing to do with how import works.  It's an
> > execution order problem.
>
> > Short version: yes, the problem is that Page doesn't exist when Book
> > is defined.  However, it has nothing to do with imports.
>
> > Long version:
>
> > Import reads and executes statements from a file and makes the names
> > defined by executing those statements available.
>
> > Python's class definitions are actually executable statements.  (The
> > organization ofhttp://docs.python.org/reference/reflectsthis
> > reality.)
>
> > A Python class statement executes some other statements in a context,
> > some of which produce executable objects, applies some transformations
> > to produce a class object, and binds that object to a variable,
> > creating that variable if necessary.
>
> > Python's executable objects can contain references to undefined
> > things.  However, some of the db.Model transformations for
> > db.ReferenceProperties depend on the specified reference_class (also a
> > db.Model) having gone through that process previously.
>
> > On Oct 15, 8:00 am, Dado <[EMAIL PROTECTED]> wrote:
>
> > > Thanx for the clarification. I knew Python doesn't "import" like other
> > > languages, and it is probably a more advanced solution. But doing it
> > > the simpler way (Ruby?) allows for better file organization (for my
> > > taste), with one file per model. I solved my problem by importing the
> > > join model inside methods that use it, but it feels like a non-
> > > efficient hack to me.
>
> > > One question though. Is it right that the example shown in the first
> > > post doesn't work because, the way Python "imports", Page doesn't yet
> > > exists when referenced by Book?
>
> > > Dado
>
> > > On Oct 15, 5:47 am, Andy Freeman <[EMAIL PROTECTED]> wrote:
>
> > > > Using multiple files for dbModel subclass definitions during
> > > > development runs into the "GAE/python doesn't reload what hasn't
> > > > changed" problem.
>
> > > > If db.Model A is defined in fileA, db.Model B is defined in fileB and
> > > > B has a db.ReferenceProperty to A, B's definition will generate errors
> > > > when you change fileB and don't restart the development server.  Why?
> > > > Because B's db.ReferenceProperty to A actually modifies A, and that
> > > > modification looks for conflicting modifications and can't distinguish
> > > > a conflicting modification from B's redefinition.  (Since fileA hasn't
> > > > changed, fileA won't be reloaded so A's previous definition will still
> > > > be around.)
>
> > > > BTW, Python doesn't import classes.  The "import" statement executes
> > > > files and makes available a cached copy of the values computed during
> > > > the execution of said files.  The "from" form of the import statement
> > > > has some special risks and "from {file} import *" is typically more
> > > > trouble than it is worth.
>
> > > > For how "fileA imports fileB and fileB imports fileA" works, consult a
> > > > Python reference.  It can be used to do certain things but not others,
> > > > and how it works depends on which file is imported first by some other
> > > > file.  If both fileA and fileB are imported by other files, "first" in
> > > > the development environment depends on which of them has changed. If
> > > > your application imports those other files depending on the request,
> > > > first also depends on request order.  (No, import and class
> > > > definitions aren't special - they're just statements that get
> > > > executed.)
>
> > > > In general, you'll be happier if you avoid "fileA imports fileB and
> > > > fileB imports fileA".  It doesn't work like other languages that
> > > > you've used before.  And, you were likely doing it in those other
> > > > languages to do something that is best done some other way in Python.
>
> > > > Organize your inter and intra file dependencies in a DAG.  Use
> > > > separate files for separatable "concepts".  If two concepts are
> > > > mutually dependent, they're probably not separable.
>
> > > > Concepts aren't classes.
>
> > > > On Oct 14, 8:31 am, Dado <[EMAIL PROTECTED]> wrote:
>
> > > > > I am having the same problem when defining a join model between two
> > > > > models... because they "depend" on each other, but in my case is a
> > > > > problem related to how Python "imports" classes (I am using "import"
> > > > > to pull in models definitions that are in different files). Does the
> > > > > code that you posted all reside in one file? Regardless, Python (I
> > > > > think, but don't trust me on that) loads class and module definitions
> > > > > as it reads (or import) the file, which means that when it first
> > > > > encounters Book (with a Page ref in it) it doesn't know what Page is.
> > > > > I haven't yet found a solution to this... please let me know if you
> > > > > do.
>
> > > > > On Oct 10, 1:06 pm, Andy Freeman <[EMAIL PROTECTED]> wrote:
>
> > > > > > Yes, but then the db.ReferenceProperty won't verify that it's being
> > > > > > pointed to the right kind of thing.
>
> > > > > > On Oct 9, 4:40 pm, yejun <[EMAIL PROTECTED]> wrote:
>
> > > > > > > You don't have to give class for ReferenceProperty.
> > > > > > > first = db.ReferenceProperty()
>
> > > > > > > On Oct 9, 6:53 pm, acuth <[EMAIL PROTECTED]> wrote:
>
> > > > > > > > Is it possible to have two models reference each other? I can't 
> > > > > > > > get
> > > > > > > > this to work and haven't been able to find this limitation in 
> > > > > > > > the
> > > > > > > > documentation. As a simple example, consider:
>
> > > > > > > > class Book(db.Model):
> > > > > > > >         title = db.StringProperty()
> > > > > > > >         first = db.ReferenceProperty(Page)
>
> > > > > > > > class Page(db.Model):
> > > > > > > >         text = db.TextProperty()
> > > > > > > >         next = db.SelfReferenceProperty()
> > > > > > > >         book = db.ReferenceProperty(Book)
>
> > > > > > > > which generates "NameError: name 'Page' is not defined" when
> > > > > > > > processing the Book class definition. Is this really a 
> > > > > > > > limitation of
> > > > > > > > the underlying data store or is it more related to how models 
> > > > > > > > are
> > > > > > > > defined in Python?
>
> > > > > > > > I'm guessing the solution is to record the 'first page in book'
> > > > > > > > information as a separate class, for example:
>
> > > > > > > > class FirstPage(db.Model):
> > > > > > > >         book = db.ReferenceProperty(Book)
> > > > > > > >         first = db.ReferenceProperty(Page)
>
> > > > > > > > any pointers would be gratefully received, Adrian- Hide quoted 
> > > > > > > > text -
>
> > > > > > > - Show quoted text -- Hide quoted text -
>
> > > > > - Show quoted text -- Hide quoted text -
>
> > > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to