views:

55

answers:

1

I get:

ImportError: cannot import name Image (from image_blob.py)

please help me thanks :s

my code:

image.py:

from google.appengine.ext import db
from app.models.item import Item

class Image(Item):
    # imports
    from app.models.image_blob import ImageBlob
    #from app.models.user import User
    #from list_user import ListUser # is needed in order to have the references

    # references
    #uploaded_by_user = db.ReferenceProperty(User, required = True)
    large_image = db.ReferenceProperty(ImageBlob, required = True)
    small_image = db.ReferenceProperty(ImageBlob, required = True)

    # image info
    title = db.StringProperty(required = True)
    description = db.StringProperty(required = False)

    # metadata


    # relations

image_blob:

from google.appengine.ext import db


class ImageBlob(db.Model):
    from app.models.image import Image

    data = db.BlobProperty(required = True)
    image = db.ReferenceProperty(Image, required = True)
+1  A: 

You're trying to import from image_blob.py before the entirety of image.py is processed. At the time which the from app.models.item import Item occurs, class Image hasn't yet been defined, and thus can't yet be imported (the entire class definition must have been processed before the symbol is actually defined).

There's a simple solution to this: Don't define the image property on ImageBlob. AppEngine's models automatically define a backwards reference for you, so when you add the ImageBlob to the Image, it'll automatically define a property on the ImageBlob which references back to the set of Images which reference it (which, in your current use case, should be of size 1).

Amber
The backwards reference is simply a query against the referencing property for itself. I agree to not create refs in both directions.
kevpie