Pymongo对象没有属性'remove'

时间:2013-05-19 06:12:13

标签: python mongodb flask pymongo mongokit

我已经从flask-mongokit exmaple修改了list.html模板。

添加了名为“ck”的“复选框”和删除链接。

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

以下是错误消息:

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

我不确定为什么Collections.Remove()方法不起作用。

1 个答案:

答案 0 :(得分:0)

好的,最后我想通了。我使用mongokit delete方法代替Pymongo从collections类中删除方法。 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中的任务:task.delete()返回重定向(url_for('show_all'))