简体   繁体   中英

Pymongo object has no attribute 'remove'

I have modified list.html template from flask-mongokit exmaple.

Added "check boxes" with name "ck" and a delete link.

    {% extends "base.html" %}
        {% block body %}
        <h2>All Items</h2>
        <ul>
            <form id="my_form" action="delete" method=get>
            {% for task in tasks %}
            <li><input type="checkbox" name="ck" value="{{ task._id }}"><a href="{{ url_for('show_task', task_id=task._id) }}" >{{ task.title }}</a> - Created: {{ task.creation.strftime('%Y-%m-%d %H:%M') }}</li>
            {% endfor %}
        </ul>
            <a href="{{ url_for('new_task') }}">Add New Task</a> <br>
            <a href="javascript:{}" onclick="document.getElementById('my_form').submit(); return false;">Delete Selected Task</a>
            </form>
        {% endblock %}

--------------------------------

from datetime import datetime

from flask import Flask, request, render_template, redirect, url_for
from flask.ext.mongokit import MongoKit, Document
import bson

app = Flask(__name__)


class Task(Document):
    __collection__ = 'tasks'
    structure = {
        'title': unicode,
        'text': unicode,
        'creation': datetime,
    }
    required_fields = ['title', 'creation']
    default_values = {'creation': datetime.utcnow()}
    use_dot_notation = True

db = MongoKit(app)
db.register([Task])

@app.route('/delete', methods=["GET", "POST"])
def task_delete():         
    if request.method == 'GET': 

        order = request.args.getlist('ck')           

        for id in order:          
            db.Task.remove({'_id':bson.ObjectId(oid=str(id))})
        return redirect(url_for('show_all'))

The following is the error message:

File "C:\Users\krisk\My Documents\Aptana Studio 3 Workspace\flask-mongokit\example\todo.py", line 58, in task_delete
db.Task.remove({'_id':bson.ObjectId(oid=str(id))})
File "C:\Python27\lib\site-packages\mongokit\schema_document.py", line 379, in __getattr__
return dict.__getattribute__(self, key)
**AttributeError: 'CallableTask' object has no attribute 'remove'**

I am not sure why Collections.Remove() method is not working.

Ok, finally I figured out. I used mongokit delete method instead of Pymongo remove method from collections class. def task_delete(): if request.method == 'GET': selected = request.args.getlist('ck') for id in selected: tasks = db.Task.find({'_id':bson.ObjectId(oid=str(id))}) for task in tasks: task.delete() return redirect(url_for('show_all'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM