python - Pymongo object has no attribute 'remove' -
i have modified list.html template flask-mongokit exmaple.
added "check boxes" name "ck" , delete link.
{% extends "base.html" %} {% block body %} <h2>all items</h2> <ul> <form id="my_form" action="delete" method=get> {% 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 %} -------------------------------- datetime import datetime flask import flask, request, render_template, redirect, url_for 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') id in order: db.task.remove({'_id':bson.objectid(oid=str(id))}) return redirect(url_for('show_all'))
the following 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 not sure why collections.remove() method not working.
ok, figured out. used mongokit delete method instead of pymongo remove method collections class. def task_delete(): if request.method == 'get': selected = request.args.getlist('ck') id in selected: tasks = db.task.find({'_id':bson.objectid(oid=str(id))}) task in tasks: task.delete() return redirect(url_for('show_all'))
Comments
Post a Comment