Skip to content

Instantly share code, notes, and snippets.

@ielshareef
Created June 25, 2012 04:13
Show Gist options
  • Save ielshareef/2986459 to your computer and use it in GitHub Desktop.
Save ielshareef/2986459 to your computer and use it in GitHub Desktop.
Error on Django-nonrel and MongoDB: AutoField (default primary key) values must be strings representing an ObjectId on MongoDB (got u'1' instead). Please make sure your SITE_ID contains a valid ObjectId string.

Fixing SITE_ID for Django-nonrel and MongoDB

by Ismail Elshareef

Django's database setting are by default set to work with relational databases. Therefore, certain issues start popping up when you work with noSQL databases, like MongoDB.

Problem

If you're running Django-nonrel with Mongodb, you will run into an issue when you execute the following command:

$ ./manage.py syncdb

The problem is, the SITE_ID in your settings.py needs to be set to something else to work with non-relational databases.

Solution

  • Create a site entry
python ./manage.py shell
>>> from django.contrib.sites.models import Site
>>> Site().save()
  • Grab the id of the Site record just created. Copy that value and put it in your settings.py.
>>> Site.objects.all()[0].id
u'4fe7aa759e654e2662000000'
  • If you're working on Heroku, you'll want to set the same SITE_ID there too
$ heroku run python manage.py shell
>>> from django.contrib.sites.models import Site
>>> Site(id=u'4fe7aa759e654e2662000000').save()
>>> Site.objects.all()[0].id
u'4fe7aa759e654e2662000000'
@rahulkp220
Copy link

Thanks, this worked well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment