Bulk delete?

10 views
Skip to first unread message

Ned Batchelder

unread,
Jan 13, 2006, 2:12:18 PM1/13/06
to Django users
I'm used to writing SQL statements like:

delete from app_things where type = 'foo';

to delete a number of objects at once. I don't see a way to do this
type of delete in Django. I was hoping for something like:

things.delete(type__exact='foo')

Certainly I could do this:

for t in things.get_list(type__exact='foo'):
t.delete()

but this incurs a lot more database activity.

Have I overlooked something?

--Ned.

Jacob Kaplan-Moss

unread,
Jan 13, 2006, 2:41:53 PM1/13/06
to django...@googlegroups.com

Nope - *we've* overlooked something; this totally should be
possible. I've opened a ticket so we can keep track of this (http://
code.djangoproject.com/ticket/1219).

Jacob

Kenneth Gonsalves

unread,
Jan 13, 2006, 9:17:52 PM1/13/06
to django...@googlegroups.com
On Saturday 14 Jan 2006 1:11 am, Jacob Kaplan-Moss wrote:
> > Have I overlooked something?
>
> Nope - *we've* overlooked something; this totally should be  
> possible.  I've opened a ticket so we can keep track of this
> (http:// code.djangoproject.com/ticket/1219).

and, while on this note, check boxes in admin for bulk delete there

--
regards
kg

http://www.livejournal.com/users/lawgon
tally ho! http://avsap.org.in
ಇಂಡ್ಲಿನಕ್ಸ வாழ்க!

Ivan Fedorov

unread,
Jan 14, 2006, 3:50:38 AM1/14/06
to django...@googlegroups.com
Kenneth Gonsalves пишет:

> On Saturday 14 Jan 2006 1:11 am, Jacob Kaplan-Moss wrote:
>
>>>Have I overlooked something?
>>
>>Nope - *we've* overlooked something; this totally should be
>>possible. I've opened a ticket so we can keep track of this
>>(http:// code.djangoproject.com/ticket/1219).
>
>
> and, while on this note, check boxes in admin for bulk delete there
>
+1

Amit Upadhyay

unread,
Jan 14, 2006, 4:02:19 AM1/14/06
to django...@googlegroups.com
On 1/14/06, Kenneth Gonsalves <law...@thenilgiris.com> wrote:

On Saturday 14 Jan 2006 1:11 am, Jacob Kaplan-Moss wrote:
> > Have I overlooked something?
>
> Nope - *we've* overlooked something; this totally should be
> possible. I've opened a ticket so we can keep track of this
> (http:// code.djangoproject.com/ticket/1219).

and, while on this note, check boxes in admin for bulk delete there

I am pretty sure check box bulk delete is going to be of the form:
for id in checked:
      things.get_object(pk=id).delete()
or something like that, which has nothing to do with "bulk delete" as being discussed in the ticket.

As of having check boxes to select multiple item in admin views, I would much rather prefer "edit selected", where you can bulk edit items, delete being just one of the operations. Its like iTunes edit multiple song screenshot attached, for each field put a checkbox before that filed, and just try to imitate what iTunes does there. [May be put a little bit of ajax and have a togglable div displaying the current vaules of objects selected]. Please tell me if I am not being clear enough.

--
Amit Upadhyay
Blog: http://www.rootshell.be/~upadhyay
+91-9867-359-701
iTunes-edit-multiple-songs.png

Amit Upadhyay

unread,
Jan 14, 2006, 4:20:30 AM1/14/06
to django...@googlegroups.com
On 1/14/06, Amit Upadhyay <upad...@gmail.com> wrote:

As of having check boxes to select multiple item in admin views, I would much rather prefer "edit selected", where you can bulk edit items, delete being just one of the operations. Its like iTunes edit multiple song screenshot attached, for each field put a checkbox before that filed, and just try to imitate what iTunes does there. [May be put a little bit of ajax and have a togglable div displaying the current vaules of objects selected]. Please tell me if I am not being clear enough.

And for paginated view where there are too many items to show on the same page, it would be good to have a admin tray to hold currently selected items for later "multi-edit", something like picasa tray, spanning multiple pages, both list and detail ones, which can hold items from different models, and clicking edit all showing a series of multi-edit pages for each model held in the admin tray.

I wish I had time to implement all this :-(
picasa-tray.png

Joey Coleman

unread,
Jan 14, 2006, 7:27:03 AM1/14/06
to django...@googlegroups.com
On 1/13/06, Ned Batchelder <ned...@gmail.com> wrote:
>
> I'm used to writing SQL statements like:
>
> delete from app_things where type = 'foo';
>
> to delete a number of objects at once. I don't see a way to do this
> type of delete in Django. I was hoping for something like:
>
> things.delete(type__exact='foo')
>

I think that's a good idea, but I'm worried about the behaviour of
things.delete() with no parameters: would it just delete all of the
things? Perhaps it should require that there is at least one
parameter.

--joey

Eugene Lazutkin

unread,
Jan 14, 2006, 2:39:58 PM1/14/06
to django...@googlegroups.com
Kenneth Gonsalves wrote:
> On Saturday 14 Jan 2006 1:11 am, Jacob Kaplan-Moss wrote:
>>> Have I overlooked something?
>> Nope - *we've* overlooked something; this totally should be
>> possible. I've opened a ticket so we can keep track of this
>> (http:// code.djangoproject.com/ticket/1219).
>
> and, while on this note, check boxes in admin for bulk delete there

I want to add one more item to the admin wish list: a button to clear
the whole cache, if any.

Thanks,

Eugene

Russell Keith-Magee

unread,
Jan 16, 2006, 9:11:53 AM1/16/06
to django...@googlegroups.com
> > things.delete(type__exact='foo')
...

> > Have I overlooked something?
>
> Nope - *we've* overlooked something; this totally should be
> possible. I've opened a ticket so we can keep track of this (http://
> code.djangoproject.com/ticket/1219).

I've just submitted a patch on magic-removal to this ticket that
implements bulk delete; the syntax is similar to that originally
proposed, except that the delete method is on the Manager, rather than
the class itself:

Things.objects.delete(type__exact='foo')

Any query that is legal for get_list() et al should be legal in
delete(), with the exception of anything requiring a join. DELETE FROM
foo INNER JOIN bar... isn't legal, so any query that requires a join
will cause the delete() to throw an exception.

Any objections to this commit? Or are the impending descriptor changes
going to render this patch immediately obsolete?

Russ Magee %-)

Russell Keith-Magee

unread,
Jan 17, 2006, 7:15:09 AM1/17/06
to django...@googlegroups.com
Ok; in the absence of any objections, I've just committed the change,
and closed off the ticket. Bulk delete is now available in
magic-removal.

Russ Magee %-)

Simon Willison

unread,
Jan 18, 2006, 3:43:03 AM1/18/06
to django...@googlegroups.com

What happens if you call Something.objects.delete() without any
arguments? Does it delete every row from the table?

If so it might be useful to have some kind of "magic" keyword
argument which has to be used to achieve that. Having a method that
can nuke everything if you hit the wrong key makes me a little
nervous. Something like this perhaps:

Something.objects.delete(DELETE_ALL=True)

Calling the delete() method with no parameters could raise an error.

Cheers,

Simon

Russell Keith-Magee

unread,
Jan 18, 2006, 8:26:55 AM1/18/06
to django...@googlegroups.com
> What happens if you call Something.objects.delete() without any
> arguments? Does it delete every row from the table?

Why? Are you saying spontaneous accidental destruction of the entire
table would be a _bad_ thing? :-)

> Something.objects.delete(DELETE_ALL=True)
>
> Calling the delete() method with no parameters could raise an error.

Sounds good to me. Implemented, tested and committed, along with
documentation to explain the bulk delete call.

Russ Magee %-)

hugo

unread,
Jan 18, 2006, 8:50:00 AM1/18/06
to Django users
>Ok; in the absence of any objections, I've just committed the change,
>and closed off the ticket. Bulk delete is now available in
>magic-removal.

Hum. That's a _tad_ bit fast committing for me - just because nobody
commented on it doesn't say there aren't any comments on it in the
queue :-)

One problem I see is with related objects. When Django does a .delete()
on an object, it does delete related objects with it. And it deletes
especially used rows in relation tables (those that are behind the
ManyToMany machinery). Does your patch handle this? I don't think so -
so when using the bulk delete, there might be leftover related objects
that aren't deleted.

bye, Georg

Adrian Holovaty

unread,
Jan 18, 2006, 9:33:10 AM1/18/06
to django...@googlegroups.com
On 1/18/06, hugo <g...@hugo.westfalen.de> wrote:
> >Ok; in the absence of any objections, I've just committed the change,
> >and closed off the ticket. Bulk delete is now available in
> >magic-removal.
>
> Hum. That's a _tad_ bit fast committing for me - just because nobody
> commented on it doesn't say there aren't any comments on it in the
> queue :-)

Yeah, that's a tad bit fast for me, as well. A lack of comments
doesn't give consent.

> One problem I see is with related objects. When Django does a .delete()
> on an object, it does delete related objects with it. And it deletes
> especially used rows in relation tables (those that are behind the
> ManyToMany machinery). Does your patch handle this? I don't think so -
> so when using the bulk delete, there might be leftover related objects
> that aren't deleted.

The patch doesn't appear to handle this, which means we have
inconsistent behavior between bulk delete and normal deletion.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Russell Keith-Magee

unread,
Jan 18, 2006, 6:31:54 PM1/18/06
to django...@googlegroups.com
On 1/18/06, Adrian Holovaty <holo...@gmail.com> wrote:

On 1/18/06, hugo <g...@hugo.westfalen.de> wrote:
> >Ok; in the absence of any objections, I've just committed the change,
> >and closed off the ticket. Bulk delete is now available in
> >magic-removal.
>
> Hum. That's a _tad_ bit fast committing for me - just because nobody
> commented on it doesn't say there aren't any comments on it in the
> queue :-)

Yeah, that's a tad bit fast for me, as well. A lack of comments
doesn't give consent.

Ok; fair comment. I was working on the assumption that there had been a reasonable amount of activity on the Django-developer list that day, so all the main players were around, and if there was a comment to be made, someone would have at least said 'not sure.. lets look into it'. Coupled with the fact that the patch seemed reasonably trivial to me - of course, I missed related tables....

Would the right approach been to wait longer, or to ask somewhere else?

The patch doesn't appear to handle this, which means we have
inconsistent behavior between bulk delete and normal deletion.

Ok; I overlooked that bit. I'll see what I can do.

Do you want me to roll out the bulk delete implementation in the interim?

Russ Magee %-)
Reply all
Reply to author
Forward
0 new messages