Django MongoDB Engine error when running tellsiteid
Asked Answered
H

9

6

SO I created a django project and app as per the tutorial and I have all the dependencies necessarry for MongoDB Engine it all seemed to be working fine and dandy till I tried enabling the admin interface.

I uncommented the require bits, and added 'django_mongodb_engine' and 'djangotoolbox' to the apps section in settings.py

When I try to get into localhost:8000/admin I get an error:

"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."

After some googling apparently I have to run manage.py tellsiteid and it'll spit me an ID I can use within my settings.py that will make the error go away, but when I try to run manage.py tellsiteid I get:

Traceback (most recent call last):
File "./manage.py", line 14, in <module>
execute_manager(settings)
File "/Users/holografix/.virtualenvs/django_nonrel_env/lib/python2.7/site-   packages/django/core/management/__init__.py", line 438, in execute_manager    utility.execute()
File "/Users/holografix/.virtualenvs/django_nonrel_env/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/holografix/.virtualenvs/django_nonrel_env/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Users/holografix/.virtualenvs/django_nonrel_env/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute
output = self.handle(*args, **options)
File "/Users/holografix/.virtualenvs/django_nonrel_env/lib/python2.7/site-packages/django/core/management/base.py", line 351, in handle
return self.handle_noargs(**options)
File "/Users/holografix/.virtualenvs/django_nonrel_env/lib/python2.7/site-packages/django_mongodb_engine/management/commands/tellsiteid.py", line 8, in handle_noargs
site_id = self._get_site_id()
File "/Users/holografix/.virtualenvs/django_nonrel_env/lib/python2.7/site-packages/django_mongodb_engine/management/commands/tellsiteid.py", line 19, in _get_site_id
return Site.objects.get().id
File "/Users/holografix/.virtualenvs/django_nonrel_env/lib/python2.7/site-packages/django/db/models/manager.py", line 132, in get
return self.get_query_set().get(*args, **kwargs)
File "/Users/holografix/.virtualenvs/django_nonrel_env/lib/python2.7/site-packages/django/db/models/query.py", line 351, in get
% self.model._meta.object_name)
django.contrib.sites.models.DoesNotExist: Site matching query does not exist.
Hauteur answered 11/1, 2012 at 12:41 Comment(0)
U
5

You haven't created any Site yet. Run manage.py syncdb to create one.

Underexpose answered 11/1, 2012 at 19:12 Comment(1)
manage.py syncdb raises the same exception even after dropping the database and starting fresh. This worked for me: 1) drop the database 2) create a Site as per https://mcmap.net/q/1583357/-django-mongodb-engine-error-when-running-tellsiteid 3) add SITE_IDto settings 4) run manage.py syncdbConsuetudinary
G
12

You can create your site, and then, get the id:

python ./manage.py shell

>>> from django.contrib.sites.models import Site
>>> s = Site()
>>> s.save()

and then:

python ./manage.py tellsiteid
Gospodin answered 20/3, 2012 at 3:28 Comment(1)
So, what if we accidentally ran this more than once, can you clear it?Chiapas
G
12

If you do not need the sites functionality (which is very probable) simply turn off django.contrib.sites app and it will fix MongoDB issues related to the SITE_ID:

INSTALLED_APPS = (   
    (...)
    # 'django.contrib.sites',  # Comment this out
    (...)
)
Generic answered 22/1, 2013 at 19:49 Comment(1)
Very usefull for me as simple as it sounds! Got a mess before that! Thanks a lotPanchito
U
5

You haven't created any Site yet. Run manage.py syncdb to create one.

Underexpose answered 11/1, 2012 at 19:12 Comment(1)
manage.py syncdb raises the same exception even after dropping the database and starting fresh. This worked for me: 1) drop the database 2) create a Site as per https://mcmap.net/q/1583357/-django-mongodb-engine-error-when-running-tellsiteid 3) add SITE_IDto settings 4) run manage.py syncdbConsuetudinary
P
3

have you tried this: http://django-mongodb.org/troubleshooting.html#site-id-issues

Paravane answered 11/1, 2012 at 12:53 Comment(0)
E
3

For some reason none of the solutions here worked for me. python ./manage.py tellsiteid, there was no django_site collection, and commenting out 'django.contrib.sites' caused weird errors.

Grabbing the ID from the shell worked for me though, detailed here:

https://gist.github.com/ielshareef/2986459 or here Site matching query does not exist

python ./manage.py shell
>>> from django.contrib.sites.models import Site
>>> Site().save()
>>> Site.objects.all()[0].id
u'4fe7aa759e654e2662000000'

Put it in settings.py and everything worked great!

Exigible answered 22/10, 2013 at 4:50 Comment(0)
M
2

I ran into this during my setup the other day.

Basically you need to logo into a mongo shell and lookup your siteid then add it your settings.py

log into a mongo shell

mongo

select your db

use *name*

then do a find() on django_site

db.django_site.find()

Then open your settings.py and edit the site_ID ="" line (mine is below)

SITE_ID = u'4f1f82011d41c81e5c00001d'

That should get you up and running

Mixologist answered 2/2, 2012 at 11:33 Comment(0)
S
2

Most likely you havent created a site yet, to do so you need to run the command

python manage.py syncdb

This creates the site, now you need to add its site_id in your settings file. Go get the site id, connect to mongodb engine that is running, and run the following commands

use mydatabase --/# or whatever name you have for your database.
db.django_site.find()

you will get something like

ObjectId("4f4e968adea3b3b30c00001d")

then in your settings file, put

site_id = u'4f4e968adea3b3b30c00001d'

and the admin interface should work. Does it?

Someplace answered 29/2, 2012 at 22:30 Comment(0)
G
0

I use manage.py syncdb and then manage.py tellsiteid but still display error.

I finally solve it by dropping the database and sync again.

Hope this can help someone:)

Glyceric answered 9/3, 2012 at 19:56 Comment(0)
S
0

sudo python manage.py shell

from django.contrib.sites.models import Site
Site().save()
Site.objects.all()[0].id
u'53aa6456984edd0d5e547e03'

Put it in settings.py SITE_ID = u'53aa6456984edd0d5e547e03'

Statesman answered 25/6, 2014 at 6:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.