0

I need some help with MongoAlchemy. I'm trying to create a web application with python, flask, Mongo DM and Mongo Alchemy (as an object document mapper) and I'm struggling with updating existing documents.

My problem is that I cannot update an existing document through it's object Id. Below I'm attaching my def for updating

@app.route('/update', methods =[ 'GET', 'POST' ])    
 def update():
     if request.method == 'POST':
         id = request.form['object_id'] # Getting values with html
         patientzero = BloodDoonor.query.get_or_404(id) 
         first_name = request.form['name']# Getting values with htmlform
         last_name = request.form['surname']# Getting values with html form
         blood_type = request.form['blood_type']# Getting values with html                
         update_record = BloodDoonor.patientzero.update(BloodDoonor.last_name = last_name)
     return render_template('update.html', result = result)

And flask gives me that error:

AttributeError AttributeError: type object 'BloodDoonor' has no attribute 'patientzero'

I'm very new to Python and not very good in code. Please forgive me for the sloppy description I gave above. Any help would be appreciated.

tim
  • 1,454
  • 1
  • 25
  • 45
alpha93
  • 1
  • 3

1 Answers1

2

To update an existing document just change the value of the object which you queried from db with form values and then just save that object:

@app.route('/update', methods =[ 'GET', 'POST' ]) 
def update(): 
    if request.method == 'POST':
        id = request.form['object_id']
        patientzero = BloodDoonor.query.get_or_404(id) 
        patientzero.first_name = request.form['name']
        patientzero.last_name = request.form['surname']
        patientzero.blood_type = request.form['blood_type']

        patientzero.save()
    return render_template('update.html')
metmirr
  • 4,234
  • 2
  • 21
  • 34