0

I am trying to use mongodb with my application, I have made a Player Class instance such as:

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

After this, I have my setters and getters for Player attributes, I set all these attributes as strings. After doing all that I want to save this Player instances to a database so that I can reach them later on. In my main python file I have this block of code :

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')

When I run the main application, It gives me the following error:

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

Tolga Oguz
  • 142
  • 1
  • 14

1 Answers1

1

This is not the way to use MongoEngine, your class must inherit from the Document base class, if you don’t your class and instances will be missing most of the internals required by MongoEngine to work fine.

EDIT: You must also define your fields as class attributes, not in the constructor. This is because of how the metaclass (of Document) works behind the scene, it scans your class attribute when the class is interpreted and that is where it expects to find your fields.

Make sure to check the example from the readme and the docs

E.g:

from mongoengine import Document, StringField, IntField

class Player(Document):
    name = StringField()
    height = IntField()
bagerard
  • 5,681
  • 3
  • 24
  • 48
  • Sorry, I forgot to add it to the code but it actually is like : class Player(mongoengine.Document): so it's still giving me the same error – Tolga Oguz Aug 14 '19 at 18:17
  • I've edited my answer, see the comment about using class attribute for the fields – bagerard Aug 14 '19 at 18:33
  • and btw, normally you don't need constructors for mongoengine Document classes but if you end up needing that, you must call the super constructor in it – bagerard Aug 14 '19 at 18:36