4

I'm developing a django application with some complicated user interactions, so I need to do a lot of testing. Is there an easy way to clear out the Users table (and all associated tables) in the database to start fresh? Also, is there a good way to autopopulate the database with "test users" to play around with?

Details:

  • This is an operation I expect to carry out several times, so it'd be nice to be able to run it quickly from the command line.
  • I'm using the basic Users model (django.contrib.auth.models.User) in django 1.3.1
  • I'm not using the admin pages, and would rather keep it that way, unless things get really desparate.

Thanks!

Abe
  • 22,738
  • 26
  • 82
  • 111

1 Answers1

7

for auto-populating, have a look at django fixtures

loading fixtures will overwrite changes, but not delete any additions. to clear the table, you want

User.objects.all().delete() 

This will also propage to anything with foreign keys referring to users. To do this from the command line, wrap this in a management command

second
  • 28,029
  • 7
  • 75
  • 76
  • Related question -- is there any way to reset the keys in the User index table so that the index of the next User created will be pk=1? It seems that User.objects.all.delete() deletes table entries, but doesn't reset the inedx. – Abe Nov 16 '11 at 16:12
  • hm. not sure. i guess you could always drop the table and run `syncdb` again. (`syncdb` leaves any existing tables alone) – second Nov 16 '11 at 16:15