Python problem with circular reference:
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)
开发者_如何学JAVA # 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)
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 Image
s which reference it (which, in your current use case, should be of size 1).
精彩评论