Search code examples
djangoresetdjango-users

How can I reset and prepoulate all users in django?


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!


Solution

  • 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