Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 395
Abe Avatar asked Sep 07 '25 18:09

Abe


1 Answers

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

like image 73
second Avatar answered Sep 10 '25 06:09

second