Search code examples
djangomongodbdjango-nonrel

Unique index for User.username in django-nonrel mongodb?


I'm building an application in django-nonrel with mongoDB as the backend. I've been trying to use the out-of-the box User authentication system, but I keep running into weirdness.

The latest problem is creating users. When I call User.objects.create_user, there doesn't seem to be any validation for uniqueness. I can create more than one user with the exactly same username. I had expected that username would enforce a unique index, but that doesn't seem to be the case.

If this were one of my own models, I'd try adding "unique=True" in the model, then syncing the DB to implement the index. Since I'm using a built-in model, I'm not sure how to proceed.

Another possibility might be to hard-code syncdb to add the index. That seems kludgy, though. What's the best way to go forward here?


Solution

  • username already has unique=True. You need to run syncdb in order to install the MongoDB indices.