开发者

Python problem with circular reference:

开发者 https://www.devze.com 2023-01-23 01:49 出处:网络
I get: ImportError: cannot import name Image (from image_blob.py) please help me thanks :s my code: image.py:

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 Images which reference it (which, in your current use case, should be of size 1).

0

精彩评论

暂无评论...
验证码 换一张
取 消