Mezzanine 4.0.1 - python manage.py createdb always create sqlite - django

This is happening with Mezzanine 4.0.1 on Ubuntu, in venv. Python 3.4.3
After creating a Mezzanine project, I changed the database connection settings in settings.py as:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": "mezzaninedb",
"USER": "mezzanine",
"PASSWORD": "mezzaninepwd",
"HOST": "localhost",
"PORT": "5432",
}
}
I have created (in Postgresql) a Mezzanine user with password mezzaninepwd and database mezzaninedb, and I "GRANT ALL PRIVILEGES ON DATABASE mezzaninedb TO mezzanine;"
Than, I run:
python manage.py createdb
It displays in terminal everything that it should display, asks me to create user, email, password...
After it finished, i "psql" into postgresql and connect to mezzaninedb, but no tables are created in that (PostgreSQL) database.
However, dev.db is created in "local dir" and when I run
python manage.py runserver
I can see mezzanine UI, but mezzanine is using Sqlite.
This happens even if I completely remove this from settings.py
#DATABASES = {
#"default": {
# "ENGINE": "django.db.backends.postgresql_psycopg2",
# "NAME": "mezzaninedb",
# "USER": "mezzanine",
# "PASSWORD": "mezzaninepwd",
# "HOST": "localhost",
# "PORT": "5432",
#}
#}
I even tried to completely "literally remove" this section from settings.py and run
python manage.py createdb
it created all tables, but in sqlite - dev.db.
So, It never creates postgresql tables, but always creates sqlite db and tables, even if DATABASES {...} is not present.
I suspected that this might be DJANGO problem, so I created django project and django app, and some models.
After using the same DATABASE {...} in settings.py, and run
python manage.py syncdb
Tables were created in POSTGRESQL !!!
Just to mention, I tried the same
python manage.py syncdb
in mezzanine, and it behaved the same as createdb - did not create anything in PostgreSQL.
Is this known mezzanine problem or I am doing something very very wrong ?

Related

Heroku doesnt migrate models on Django

I just deploy my Django app to Heroku but I cant migrate my migrations to heroku. First I run :
heroku run python manage.py migrate all the migrations list as OK but when I showmigrations, none of them is migrating (all blank [ ]).
Then I try heroku run bash and migrate from there, everything seems ok even showmigrations from bash showing all of the migrations is working. I even manage to create a superuser. But when I open my admin page and log in with superuser it shows 'account.account' table does not exist and when I check showmigrations again all of the migrations are gone. I have been repeating this migration over and over and still can't figure this out.
Does anyone know what I am doing wrong here?
Edit :
I don't know if this is related but when I push my project to Heroku the first time, I'm using Postgre with this setting :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': DB_NAME,
'USER': DB_USER,
'PASSWORD': DB_PASSWORD,
'HOST': 'localhost',
'PORT': '5432',
}
}
but when I try to run migrations it shows an error something like cant access localhost with 5432 port, it turns out that Heroku trying to access postgre in my localhost instead of using Heroku Postgres. The solution I found is to dump my db and reload it to Heroku. And since I don't know how to set that up. I just comment that postgre setting and replace it with default django setting :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
create my dbsqlite file, running migrations again then once again push it to Heroku, hoping to find an easy way out but instead ended up in this problem
I'd recommend you to push a migrated project to heroku that is
python manage.py migrate
then push that migrated project to heroku
the reason your superuser doesn't get stored while creating superuser from heroku bash is because heroku has an ephemeral drive i.e heroku clears all the modified data from orignal push after equal interval of time.
It turns out that I forgot to install django-heroku. More on this : https://devcenter.heroku.com/articles/django-app-configuration

how to switch to a new database

I want to deploy my django project to the production environments, and associated it with an new empty database, and I did as follows :
Create an new empty database
Updated settings.py and pointed the database name to the new one
Deleted the migrations folder under my App
Run python manage.py runserver and no errors returned
Run python manage.py makemigrations and python manage.py migrate
but only auth related tables created ( like auth_user , auth_group ... ), no databases tables created for my Apps
How should I do for this situation to move to the new database for my project?
Deleted the migrations folder under my App
This was your mistake, you deleted the migrations - including the initial migrations. So when you go to makemigrations you haven't got the initial migration available.
So you need to run makemigrations <app_name> to at least get the initial migration.
If you were to do this again, don't delete the migrations, just change the database settings and then migrate.
Firstly, you should not have deleted the migrations. Now, make all the migrations again which you have deleted.
python manage.py makemigrations app_name
Do this for all the apps of which you have deleted the migrations.
Now, add your new database to settings.py. Do not remove the old one yet. For example, if I were adding a MySQL database, I would have added the following to the DATABASES dictionary in settings.py:
'new': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'databasename',
'USER': 'databaseusername',
'PASSWORD': 'databasepassword',
'HOST': 'localhost',
'PORT': '3306',
}
I have named the database as 'new'. Now we have two databases 'default' and 'new'. Now you have to create tables in the new database by running the migrations on the new database:
python manage.py migrate --database=new
You can follow these additional steps if you want to transfer your data to the new database. First, clear the new database:
python manage.py flush --database=new
Now export data from the old database into a json file:
python manage.py dumpdata>data.json
Import this data into the new database:
python manage.py loaddata data.json --database=new
Now you can remove the 'default' database and rename the 'new' database to 'default'.
The procedure mentioned in this answer is taken from my blog.
Just check the output of python manage.py makemigrations command, if it is showing no change detected then you need to check that have you added that app in your INSTALLED_APPS = [] in settings.py file or it might be the problem because you have deleted migration folder.Because if is there any database connectivity error it will show you that while doing makemigrations.
If your database has a new name, i.e. not "default", you need to specify it to migrate:
python manage.py migrate --database <newdb>

Django 1.8, syncdb not working, throwing a foreign key constraint error

Since I upgrade to Django 1.8 from 1.7, I have got this foreign key constraint error.
File "c:project\env\lib\site-packages\mysql_python-1.2.5-py2.7-win32.egg/MySQLdb\connections.py line 36, in defaulterrorhandler raise errorclass, errorvalue,
Django.db.utils.IntergrityError: 'Cannot add foreing key contraint
What's some wrong with django 1.8 (latest version)?
Try this
DATABASES = {
'default': {
...
'OPTIONS': {
"init_command": "SET foreign_key_checks = 0;",
},
'STORAGE_ENGINE': 'MyISAM / INNODB / ETC'
}
}
Have you created migrations for all your apps? If not, you may well be hitting the problem that the database tables are being created in the wrong order, which will give you this error.
If you have an existing Django 1.7 project, then you need to create the initial migration files, and then fake the initial migration, as described here
https://docs.djangoproject.com/en/1.8/topics/migrations/#adding-migrations-to-apps
Create the migration with
$ python manage.py make migrations your_app_label
And then fake the application
$ python manage.py migrate --fake-initial your_app_label

Django: MySQL no such table: aidata.django_session

I'm running Django 1.4 on Windows 7 in Pycharm and I installed WAMP because I need to have my data in a MySQL table.
This is from setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'aidata',
'USER': 'root'
}
}
From installed_apps I uncommented:
'django.contrib.sessions'
Running manage.py syncdb does not create any tables ( even models) in my mysqldb.
I get the error when trying to acces /admin/
DatabaseError at /admin/
(1146, "Table 'aidata.django_session' doesn't exist")
Double check the db credentials
make sure you uncommented this line in your middleware:
MIDDLEWARE_CLASSES = (
....
'django.contrib.sessions.middleware.SessionMiddleware',
)
then try to python manage.py syncdb.
if you are still having issues post any output
EDIT -- NEXT CHECK:
do you have a "django_content_type" table?
if so, does that table have a "session" record?
if so, delete the session record and try to python manage.py syncdb
EDIT -- STEP 3:
now i'm guessing, post up your settings file so i can make meaningful troubleshooting attempts
Stop your server if you have one running
go into your file browser and delete the settings.pyc file
try to python manage.py syncdb
my thought is that a pyc file with the sqlLite info may be cached and not regenerating
EDIT -- STEP 4:
everything in your settings.py look ok to me. try something for me? create a new django project, don't enable the admin or add in your apps i just want to know if from scratch everything in your django install seems to be working
django-admin.py startproject testsite
do the database configuration/setup
python manage.py syncdb
let me know if the models create properly
I was running into the same problem and for me (running django 1.7 development trunk of mid-sept.2013) it helped to
remove all south migrations ([app]/migration)-directories
remove south from INSTALLED_APPS in settings.py
That might be due to the shift towards the integrated migration system in django v1.7, but I'm speculating here.

Django - MongoDB: (could not connect to localhost:27017) Connection refused

I am having issues getting mongodb working with Django, my setup is Mac OS X 10.7. I followed the tutorial available here: http://django-mongodb.org/topics/setup.html . I have tried with both virtualenv and without (this is my first time installing django so I shouldn't have any conflicts).
In settings.py
DATABASES = {
'default' : {
'ENGINE' : 'django_mongodb_engine',
'NAME' : 'my_database'
}
}
In firstapp.models.py
from django.db import models
from djangotoolbox.fields import ListField
class Post(models.Model):
title = models.CharField()
text = models.TextField()
tags = ListField()
comments = ListField()
In my shell (python manage.py shell), I try running:
from mydjango.firstapp.models import Post
post = Post.objects.create();
But I keep getting the following: DatabaseError: could not connect to localhost:27017: [Errno 61] Connection refused (full traceback)
If I switch settings.py to the following:
DATABASES = {
'default': {
'ENGINE': 'django_mongodb_engine',
'NAME': 'my_database',
'USER': '',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '27017',
'SUPPORTS_TRANSACTIONS': False,
},
}
I get this error in the shell: ImproperlyConfigured: port must be an instance of int
Did you setup MongoDB separately? The howto you link doesn't seem to go over the MongoDB configuration. It assumes the database is already running. In any case MongoDB seems down, or is at least listening somewhere else.
The last error ("...instance of int") is just because you specified '27017' (a string) instead of 27017 in the configuration dictionary. But even then it should be equivalent to the first, simpler configuration.
In case MongoDB is running but you still get this error while trying to connect from another machine, it may be due to Firewall running on the MongoDB server.
I was running into this exact same error on CentOS 6.5 running MongoDB 2.6.0. Disabling firewall on the machine resolved the issue for me.
If you are creating models at models.py then there is an example below
models.py
from mongoengine import *
class UserLocation(Document):
message = StringField(required=True, max_length=200)
You dont need the sqlite3 if you are not using it and only the mongodb then you have an optio to comment it
settings.py
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
# }
Now in
views.py
from .models import UserLocation
def save_function(request):
msg = "shinto"
# saving the data to the database
data = UserLocation(message = msg)
data.save()
# reading the data
read_data = json.loads(UserLocation.objects().to_json())
print read_data
There is also another method and its very simple (you don't need to create models in models.py)
views.py
from pymongo import MongoClient
client = MongoClient(port=27017)
db = client.testing_db # use a database called testing_db
collection = db.files # inside that db a collection called files
def a_new_function():
fooman = {"name" : "shinto", "age" : 25}
collection.insert(fooman)
# if you need to display only the name "shinto
#cursor = collection.find({"name" : "shinto"})
# if you need to display all then empty
cursor = collection.find({})
for document in cursor:
print(document)
could not connect to localhost:27017 Connection refused or you see 111 is because you haven't either installed or opened mongodb in another terminal
For installation on ubuntu do the following
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
if Ubuntu 12.04 (deprecated) then in terminal
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu precise/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
if Ubuntu 14.04 then in terminal
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
if Ubuntu 16.04 then in terminal
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
After do
sudo apt-get update
sudo apt-get install -y mongodb-org
After installation then start mongodb
sudo service mongod start
To stop mongodb do
sudo service mongod stop
To restart mongodb
sudo service mongod restart
Finally you can use
mongo --host 127.0.0.1:27017
which will solve the issue
Thank You
From your question it seems like you are trying to use Django with Mongodb. In which case why do you need Mongoengine?
The official Mongodb documentation talks about djongo. It works by translating SQL queries into query documents.
You don't need Mongoengine to run it.
All native Django contrib modules (eg. admin, user, session) work without any modification.
MongoEngine requires rewriting Django modules and last I checked, the native admin module didn't run on MongoEngine.
Your existing models run without any ORM translation as well.