On Fri, Dec 4, 2009 at 3:44 PM, Daniel Roseman <[email protected]> wrote:
> On Dec 4, 8:37 pm, Phlip <[email protected]> wrote:
>> Django users:
>>
>> I only ask the question to help improve my esthetics, but others might
>> need a performance boost here.
>>
>> How to delete every record in a table?
>>
>> A related question - how to delete every record matching a filter, out
>> of one internal SQL command?
>>
>> The brute-force way:
>>
>> for p in Plutocrat.objects.all(): p.delete()
>>
>> Is there a way to do that in one method call? and could such a method
>> call only send one SQL command?
>>
>
> Yes:
> Plutocrat.objects.all().delete()
I found that quite slow. If you want to stay DB agnostic, I came up with this:
from django.db import connection, transaction
models = [Person, User, ...]
cursor = connection.cursor()
flush_tables = []
for model in models:
flush_tables.append(model._meta.db_table)
statements = connection.ops.sql_flush(no_style(),
flush_tables,
connection.introspection.sequence_list())
for statement in statements:
cursor.execute(statement)
transaction.commit_unless_managed()
I don't know of any bad side effects...
-Doug
> --
> DR.
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.