我正在使用GAE Python 2.7标准环境创建一个基于Flask的Web应用程序。当我尝试将动物模型导入应用程序/__init__py文件和其他文件时,App Engine SDK 会引发 DuplicatePropertyError。
DuplicatePropertyError: Class AnimalKind already has property animal_set
.
├── README.md
├── requirements.txt
├── src
│ ├── app.yaml
│ ├── application
│ │ ├── __init__.py
│ │ ├── animal_bp
│ │ │ ├── __init__.py
│ │ │ └── views.py
│ │ ├── appengine_config.py
│ │ ├── main.py
│ │ ├── models
│ │ │ ├── __init__.py
│ │ │ ├── base_models.py
│ │ │ ├── common_models.py
│ │ │ └── kind_models.py
│ │ └── settings.py
│ ├── index.yaml
│ ├── lib
app.yaml
service: default
runtime: python27
api_version: 1
threadsafe: true
instance_class: F1
builtins:
- appstats: on
- remote_api: on
libraries:
- name: ssl
version: latest
inbound_services:
- warmup
# [START handlers]
handlers:
- url: /static
static_dir: static
- url: .*
script: run.application.app
# script: main.app
secure: always
# [END handlers]
run.py
# -*- coding: utf-8 -*-
import os
import sys
sys.path.insert(1, os.path.join(os.path.abspath('.'), 'lib'))
sys.path.insert(1, os.path.join(os.path.abspath('.'), 'application'))
import application
应用程序/初始化.py
# -*- coding: utf-8 -*-
import os
from flask import Flask, g, request, session
from werkzeug.debug import DebuggedApplication
from animal_bp.views import animal_bp
from models.common_models import Animal
def create_app():
app = Flask(__name__)
app.config.from_object('application.settings.Local')
app.wsgi_app = DebuggedApplication(app.wsgi_app, evalex=True)
app.register_blueprint(animal_bp)
return app
app = create_app()
应用/型号/common_models.py
from google.appengine.ext import db
from models.kind_models import AnimalKind
class Animal(db.Model):
"""
Animal Model
"""
animal_kind_name = db.ReferenceProperty(AnimalKind)
应用/型号/kind_models.py
from google.appengine.ext import db
class AnimalKind(db.Model):
"""
Kind Model
"""
name = db.StringProperty(required=True)
应用程序/animal_bp/视图.py
from flask import Blueprint
from models.common_models import Animal
animal_bp = Blueprint('animal_bp', __name__)
@animal_bp.route('/')
def hello():
return 'Hello world1'
例如,如果我在animal_bp/视图中删除"从models.common_models导入动物".py则不会引发错误。当然,当我从应用程序/init.py文件中删除"从models.common_models导入动物"时,不会引发错误。
我没有任何重复的属性,但由于我的目录结构,初始化过程可能运行两次。很抱歉这么长的问题,但欢迎任何帮助!!
from google.appengine.ext import db
from models.kind_models import AnimalKind
class Animal(db.Model):
"""
Animal Model
"""
animal_kind_name = db.ReferenceProperty(AnimalKind)
错误似乎是您正在创建一个 ReferenceProperty,而没有为其指定显式名称。
from google.appengine.ext import db
from models.kind_models import AnimalKind
class Animal(db.Model):
"""
Animal Model
"""
animal_kind_name = db.ReferenceProperty(AnimalKind, name='testName')
我认为这在某些旧版本中不是必需的,但是在(1.7.6+(之后,它们改变了初始化实例的方式。
仅供参考,Google Cloud 上的 Python 2 支持已于 2020 年 1 月 1 日结束,鼓励所有开发人员尽快升级到 Python 3。
蟒蛇2
日落DuplicatePropertyError 如果有多个引用同一模型类的 ReferenceProperty 值,则会发生错误。对此的解决方案是在 ReferenceProperty 中显式设置 collection_name 参数,如下所示:
class Animal(db.Model):
name = db.ReferenceProperty(AnimalKind, collection_name='test_reference')