Delete the :class:`~mongoengine.Document` from the database. This will only take effect if the document has been previously saved. :param signal_kwargs: (optional) kwargs dictionary to be passed to the signal calls. :param write_concern: Extra keyword arguments a
(self, signal_kwargs=None, **write_concern)
| 663 | return self._qs.filter(**self._object_key).update_one(**kwargs) |
| 664 | |
| 665 | def delete(self, signal_kwargs=None, **write_concern): |
| 666 | """Delete the :class:`~mongoengine.Document` from the database. This |
| 667 | will only take effect if the document has been previously saved. |
| 668 | |
| 669 | :param signal_kwargs: (optional) kwargs dictionary to be passed to |
| 670 | the signal calls. |
| 671 | :param write_concern: Extra keyword arguments are passed down which |
| 672 | will be used as options for the resultant ``getLastError`` command. |
| 673 | For example, ``save(..., w: 2, fsync: True)`` will |
| 674 | wait until at least two servers have recorded the write and |
| 675 | will force an fsync on the primary server. |
| 676 | """ |
| 677 | signal_kwargs = signal_kwargs or {} |
| 678 | signals.pre_delete.send(self.__class__, document=self, **signal_kwargs) |
| 679 | |
| 680 | # Delete FileFields separately |
| 681 | FileField = _import_class("FileField") |
| 682 | for name, field in self._fields.items(): |
| 683 | if isinstance(field, FileField): |
| 684 | getattr(self, name).delete() |
| 685 | |
| 686 | try: |
| 687 | self._qs.filter(**self._object_key).delete( |
| 688 | write_concern=write_concern, _from_doc_delete=True |
| 689 | ) |
| 690 | except pymongo.errors.OperationFailure as err: |
| 691 | message = "Could not delete document (%s)" % err.args |
| 692 | raise OperationError(message) |
| 693 | signals.post_delete.send(self.__class__, document=self, **signal_kwargs) |
| 694 | |
| 695 | def switch_db(self, db_alias, keep_created=True): |
| 696 | """ |