mkvirtualenv -p /usr/bin/python2.7 [Project name]
pip install git+https://github.com/django-nonrel/django-nonrel.git@master
pip install git+https://github.com/django-nonrel/djangotoolbox.git@master
pip install git+https://github.com/django-nonrel/mongodb-engine.git@master
pip instal py.test
pip install django_pytest
cd [dev directory]
workon [Project name]
django-admin.py startproject [Project name]
cd [Project root]
#For application placement
mkdir apps
touch apps/__init__.py
#Test data, etc.
mkdir fixtures
#Internationalized po/mo file storage
mkdir -p local/ja/LC_MESSAGES
mkdir -p local/en/LC_MESSAGES
# Put project-specific requirements here.
# See http://pip-installer.org/requirement-format.html for more information.
mkdir requirements
# This directory is used to store static assets for
# your project. User media files (FileFields/ImageFields) are not stored here.
mkdir static
mkdir static/img
mkdir static/js
mkdir static/css
#Template storage
mkdir templates
#Document storage
mkdir docs
cd [Project root]/apps
#For model placement
django-admin.py startapp models
cd [Project root]
git config --global user.name "User name"
git config --global user.email "mail address"
git init
git add *.py
git add app
git commit -m 'first commit'
git remote add origin [email protected]:USER_ID/REPO_NAME.git
git push -u origin master
mkdir -p environment/dev/var/mongodb
mkdir -p environment/dev/etc
#Create configuration file
cat <<EOF >> environment/dev/etc/mongod.conf
heredoc> # Store data
heredoc> dbpath = [Project root]/environment/dev/var/mongodb
heredoc>
heredoc> # Only accept local connections
heredoc> bind_ip = 127.0.0.1
heredoc> EOF
#Prevent development DB from entering git
cat << EOF >> environment/dev/var/mongodb/README
heredoc>Development MongoDB data storage
heredoc> EOF
cat << EOF >> environment/dev/var/mongodb/.gitignore
heredoc> /*
heredoc> /.*
heredoc> !README
heredoc> !.gitignore
heredoc> EOF
mongod run --config environment/dev/etc/mongod.conf
mongo
> use [DB name]
> db.addUser('[DB username]', '[DB password]')
settings.py
DATABASES = {
'default': {
'ENGINE': 'django_mongodb_engine', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': '[DB name]', # Or path to database file if using sqlite3.
'USER': '[DB username]', # Not used with sqlite3.
'PASSWORD': '[DB password]', # Not used with sqlite3.
'HOST': 'localhost', # Set to empty string for localhost. Not used with sqlite3.
'PORT': 27017, # Set to empty string for default. Not used with sqlite3.
}
}
** If USER and PASSWORD are set, a login error to the DB will occur in python manage.py test
.
Therefore, it seems better to set USER ='' and PASSWORD ='' in the development environment. ** **
syncdb
python manage.py syncdb
In MongoDB, the ID must be ObjectID, but Django's SITE_ID is 1 by default, so the following error occurs.
bson.errors.InvalidId: 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.
http://django-mongodb.org/troubleshooting.html#site-id-issues
Then, you should get the ID to be set in SITE_ID with python manage.py tellsite id
, but at this point, there is no record in django_site, so I can't get it.
Therefore, you can create the site manually as follows.
python manage.py shell
>>> from django.contrib.sites.models import Site
>>> s = Site()
>>> s.save()
>>> quit()
python manage.py tellsiteid
Set the SITE_ID you got with this in settings.py.
settings.py
SITE_ID = u'XXXXXXXXXXXXXXXXX'
I thought, but in the end I couldn't prevent this error from appearing during test.
The cause is django / contrib / sites / management.py. Here, pk = 1 is forcibly inserted when creating the Site.
django/contrib/sites/management.py
def create_default_site(app, created_models, verbosity, db, **kwargs):
# Only create the default sites in databases where Django created the table
if Site in created_models and router.allow_syncdb(db, Site) :
if verbosity >= 2:
print "Creating example.com Site object"
# The default settings set SITE_ID = 1, and some tests in Django's test
# suite rely on this value. However, if database sequences are reused
# (e.g. in the test suite after flush/syncdb), it isn't guaranteed that
# the next id will be 1, so we coerce it. See #15573 and #16353. This
# can also crop up outside of tests - see #15346.
s = Site(pk=1, domain="example.com", name="example.com")
s.save(using=db)
Site.objects.clear_cache()
signals.post_syncdb.connect(create_default_site, sender=site_app)
It's a hassle, so I stopped using django.contrib.auth and django.contrib.sites with INSTALL_APPS.
settings.py
INSTALLED_APPS = (
#'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
#'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
Recommended Posts