MongoEngine Python 属性错误

MongoEngine Python AttributeError

我正在尝试在我的应用程序中使用 mongodb,我制作了一个 Player Class 实例,例如:

class Player(mongoengine.Document):

def __init__(self,name=None,hschool=None,gradclass=None,position=None,
             height=None,weight=None,main_pos=None,side_pos=None,arm=None,
            extVelo=None,infieldVelo=None,outfieldVelo=None,popTime=None,
            catcherVelo=None,sixtyYard=None,fastBallVelo=None,maxfBallVelo=None,
            curveball=None,changeup=None,slider=None,splitter=None,cutter=None,
            knuckle=None,fork=None,profile_url=None):
     self._name = mongoengine.StringField()
    self.p_name = name
    self._hschool = mongoengine.StringField()
    self._gradclass = mongoengine.StringField()
    self._position = mongoengine.StringField()
    self._main_pos = mongoengine.StringField()
    self._side_pos = mongoengine.StringField()
    self._arm = mongoengine.StringField()
    self._height = mongoengine.StringField()
    self._weight = mongoengine.StringField()
    self._extVelo= mongoengine.StringField()
    self.heightFlag = False
    self._infieldVelo = mongoengine.StringField()
    self._outfieldVelo = mongoengine.StringField()
    self._popTime = mongoengine.StringField()
    self._catcherVelo = mongoengine.StringField()
    self._sixtyYard=mongoengine.StringField()
    self._fastBallVelo = mongoengine.StringField()
    self._maxfBallVelo = mongoengine.StringField()
    self._curveball = mongoengine.StringField()
    self._changeup = mongoengine.StringField()
    self._slider = mongoengine.StringField()
    self._splitter = mongoengine.StringField()
    self._cutter = mongoengine.StringField()
    self._knuckle = mongoengine.StringField()
    self._fork = mongoengine.StringField()
    self._profile_url = mongoengine.StringField()
    self.p_profile_url = profile_url

在此之后,我有播放器属性的 setter 和 getter,我将所有这些属性设置为字符串。完成所有这些之后,我想将这个 Player 实例保存到数据库中,以便以后可以访问它们。在我的主 python 文件中,我有这段代码:

def LoadData_click(url):
    p_arr=[]
    # javascript func for clearing the list here
    try:
        table1_string,table2_string,soup,page_title = get_data(url)
    except Exception as e:
        print(e)     
        return
    p_arr = get_roster(soup)
    print('button worked')
    for i,p in enumerate(p_arr):
        print(i+1/len(p_arr)*100)
        eel.progressBarUpdate(int((i+1)/len(p_arr)*100))
        set_position(p,table2_string)
        set_pitching(p,table1_string)
        set_link_props(p)
        p.save()
        # add Item to list
    return p_arr


if __name__ == "__main__":
    mongoengine.register_connection(alias='core',name='database')

当我 运行 主应用程序时,它给我以下错误:

File "c:\Users\tolga\Desktop\PlayerEditorJS\main.py", line 33, in LoadData_click p.save() File "C:\Python\lib\site-packages\mongoengine\document.py", line 369, in save self.validate(clean=clean) File "C:\Python\lib\site-packages\mongoengine\base\document.py", line 384, in validate self._data.get(name)) for name in self._fields_ordered] File "C:\Python\lib\site-packages\mongoengine\base\document.py", line 384, in self._data.get(name)) for name in self._fields_ordered] AttributeError: _dynamic_fields

这不是使用 MongoEngine 的方式,您的 class 必须继承自 Document 基础 class,如果您不这样做,您的 class 和实例将缺少 MongoEngine 正常工作所需的大部分内部构件。

编辑: 您还必须将字段定义为 class 属性,而不是在构造函数中。这是因为 metaclass(Document 的)如何在幕后工作,它会在解释 class 时扫描您的 class 属性,这就是它期望的地方找到你的字段。

确保检查 readme and the docs

中的示例

例如:

from mongoengine import Document, StringField, IntField

class Player(Document):
    name = StringField()
    height = IntField()