如何删除对象数据

时间:2013-09-22 01:57:47

标签: django python-2.7

我正在关注Need a minimal Django file upload example

随着每次上传,列表越来越多,如何通过python shell手动删除对象数据,即文档:文档对象

>>> from myproject.myapp.models import Document

>>> Document.objects.all()

[<Document: Document object>, <Document: Document object>, <Document: Document object>, <Document: Document object>, <Document: Document object>, 

>>> Document

  <class 'myproject.myapp.models.Document'>

2 个答案:

答案 0 :(得分:2)

删除所有对象: -

Document.objects.all().delete()

删除满足某些过滤器的对象: -

Document.objects.filter(<your_filters>).delete()

删除对象,同时确保调用django信号(如果有):

for document in Document.objects.filter(<your_filters>):
    document.delete()

答案 1 :(得分:1)

remove(obj1, obj2, ...)
    Removes the specified model objects from the related object set.

请参阅此处的文档:https://docs.djangoproject.com/en/dev/topics/db/queries/