Django--HELP. django not creating sqlite database file as expected - python

New to django
following tutorial 1. also looked up all related questions on stack overflow. thought it was a absolute path issue...but absolute path seems to be correct. below is settings.py. any ideas?
MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'Users/Leerix/cars/carfilter/database/temp.db', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
Below is the error i'm getting. Been at this for several hours and cannot find the bug. any help is greatly appreciated. Thx
python manage.py syncdb
Traceback (most recent call last):
File "manage.py", line 9, in <module>
execute_from_command_line(sys.argv)
File "/Library/Python/2.5/site-packages/django/core/management/__init__.py", line 420, in execute_from_command_line
utility.execute()
File "/Library/Python/2.5/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.5/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Python/2.5/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.5/site-packages/django/core/management/base.py", line 371, in handle
return self.handle_noargs(**options)
File "/Library/Python/2.5/site-packages/django/core/management/commands/syncdb.py", line 57, in handle_noargs
cursor = connection.cursor()
File "/Library/Python/2.5/site-packages/django/db/backends/__init__.py", line 306, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/Library/Python/2.5/site-packages/django/db/backends/sqlite3/base.py", line 259, in _cursor
self.connection = Database.connect(**kwargs)
sqlite3.OperationalError: unable to open database file

Perhaps you meant to use an absolute path (one starting with /) instead of a relative path.

Delete the database file at that location. Also, the path starting with Users isn't absolute (is it?) make sure if you're on windows that it starts with a drive letter. (C:\)

Related

Django databases: problems with the test database

I am learning about Django testing.
I wrote a simple App with a simple model and would like to run tests to check the validity of a model method, but I get an error message when I run the test:
here's models.py
from django.db import models
class Trip(models.Model):
origin = models.CharField(max_length=20)
destination = models.CharField(max_length=20)
def __str__(self):
return self.origin
def is_valid(self):
return self.origin != self.destination
Here's test.py
from django.test import TestCase
from .models import Trip
# Create your tests here.
class TripModelTests(TestCase):
def test_trip(self):
a = Trip.objects.create(origin='a', destination='a')
self.assertIs(a.is_valid(), True)
here is settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'd57r9kcrhthdc7',
'USER': 'sdqxaruartlvrd',
'PASSWORD': 'e7b8f85611596ed125fe3ed4ea590f821f65e317c17ee7871be75b8130d72378',
'HOST': 'ec2-3-214-46-194.compute-1.amazonaws.com',
'PORT': '5432',
'TEST': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
}
and here is the error message i get when I run python manage.py test transport
Creating test database for alias 'default'...
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\commands\test.py", line 23, in run_from_argv
super().run_from_argv(argv)
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 371, in execute
output = self.handle(*args, **options)
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\commands\test.py", line 53, in handle
failures = test_runner.run_tests(test_labels)
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\test\runner.py", line 695, in run_tests
old_config = self.setup_databases(aliases=databases)
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\test\runner.py", line 614, in setup_databases
return _setup_databases(
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\test\utils.py", line 170, in setup_databases
connection.creation.create_test_db(
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\creation.py", line 55, in create_test_db
self._create_test_db(verbosity, autoclobber, keepdb)
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\creation.py", line 172, in _create_test_db
'dbname': self.connection.ops.quote_name(test_database_name),
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\postgresql\operations.py", line 113, in quote_name
if name.startswith('"') and name.endswith('"'):
AttributeError: 'WindowsPath' object has no attribute 'startswith'
The test works fine if I just use the default django settings and use a sqlite database....
The error could be due to your BASE_DIR path, in your settings.py, you need to remove the slash / and switch it over to the following
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
You may need to verify depending on your BASE_DIR contents that it points to the right place. One way to debugging this is to set ipdb() just after your database dictonary, so once you use python manage.py runserver, can can easily inspect the DATABASES structure.
import ipdb; ipdb.set_trace()
Source: https://pypi.org/project/ipdb/

django-nonrel syncdb and mongodb: pymongo.errors.OperationFailure

I am using django-nonrel with mongodb-engine I am getting the following error when I run python manage.py syncdb:
Traceback (most recent call last):
File "manage.py", line 22, in <module> execute_from_command_line(sys.argv)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/django/core/management/base.py", line 415, in handle
return self.handle_noargs(**options)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 147, in handle_noargs
index_sql = connection.creation.sql_indexes_for_model(model, self.style)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/django_mongodb_engine/creation.py", line 49, in sql_indexes_for_model
self._handle_oldstyle_indexes(ensure_index, meta)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/django_mongodb_engine/creation.py", line 116, in _handle_oldstyle_indexes
sparse=field.name in sparse_indexes)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/django_mongodb_engine/creation.py", line 42, in ensure_index
return collection.ensure_index(*args, **kwargs)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/pymongo/collection.py", line 1420, in ensure_index
self.__create_index(keys, kwargs)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/pymongo/collection.py", line 1298, in __create_index
sock_info, cmd, read_preference=ReadPreference.PRIMARY)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/pymongo/collection.py", line 208, in _command
read_concern=read_concern)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/pymongo/pool.py", line 239, in command
read_concern)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/pymongo/network.py", line 102, in command
helpers._check_command_response(response_doc, None, allowable_errors)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/pymongo/helpers.py", line 205, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: The field 'sparse' is not valid for an _id index specification. Specification: { ns: "qmcdb_mongodb.django_admin_log", v: 2, sparse: false, unique: true, name: "_id_1", key: { _id: 1 } }
I have no idea what is wrong. I've been trying to get mongodb to work with django and I have been having a lot of issues. My settings.py:
DATABASES = {
'default': {
'ENGINE': 'django_mongodb_engine',
'NAME': 'qmcdb_mongodb',
'PORT': 27017,
'HOST': 'localhost'
}
}
I hope I have posted enough background information. I would really appreciate any help.
Try use Mongo database in version 3.2, There is some problem with mongoengine and Mongo Server 3.4
1.Tested syncdb in MongoDB version 3.2 3.4, it still raising error.
2.In the MongoDB 2.6 syncdb is running fine with a minor unrelated problem.I still got an error in version 2.6 and fixed with this(https://gist.github.com/ielshareef/2986459).
3.django_mongodb_engine is so buggy which was forked from django1.3 . Also, this repo is outdated that last commit was on Jul 13, 2015. I should not recommend this package.
4.I would recommend https://github.com/MongoEngine/django-mongoengine
My hacky solution to this was modifying lines 115-116 of Lib\site-packages\django_mongodb_engine\creation.py
from this:
ensure_index(column, unique=field.unique,
sparse=field.name in sparse_indexes)
to this:
ensure_index(column)
It seems to clear the error, but I'm sure it's gonna make something somewhere fail catastrophically because I have no idea what I'm doing.
I'll be waiting on a better answer too.
I recommend you use:
pip3 install git+https://github.com/MongoEngine/django-mongoengine
Django 1.11.2
Python 3.6
MongoDB 3.4
Always is good, use versions updated of packages, use isolation of environment.
https://virtualenvwrapper.readthedocs.io/en/latest/install.html

Setting Up Apache Solr on Local Instance of readthedocs.org

Link to Instructions
I am following the instructions at http://read-the-docs.readthedocs.org/en/latest/install.html#solr-search-setup
Question
Currently, I am able to set up a local instance of readthedocs.org; however, I cannot set up search correctly on it as I can't seem to generate the necessary schema.xml file by running the following command. The context of which is in the link above.
./manage.py build_solr_schema > $SOLR_PATH/solr/conf/schema.xml
Instead of successfully generating a schema.xml, I am presented with this error.
Traceback (most recent call last):
File "./manage.py", line 11, in <module>
execute_from_command_line(sys.argv)
File "/home/***/ford-env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/home/***/ford-env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/***/ford-env/local/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/***/ford-env/local/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/home/***/ford-env/local/lib/python2.7/site-packages/haystack/management/commands/build_solr_schema.py", line 26, in handle
schema_xml = self.build_template(using=using)
File "/home/***/ford-env/local/lib/python2.7/site-packages/haystack/management/commands/build_solr_schema.py", line 52, in build_template
c = self.build_context(using=using)
File "/home/***/ford-env/local/lib/python2.7/site-packages/haystack/management/commands/build_solr_schema.py", line 38, in build_context
raise ImproperlyConfigured("'%s' isn't configured as a SolrEngine)." % backend.connection_alias)
I'm guessing there is an extra step involving for configuring solr with django before generating xml. Similar errors suggest editing a settings.py file, but I cannot seem to find one within the project directory. Due to my lack of familiarity with django, I don't know how to continue from here.
you have to update the settings files in readthedocs.org/readthedocs/settings/*.py and update the HAYSTACK_CONNECTIONS dict with (if you're using solr):
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.solr_backend.SolrEngine',
'URL': 'http://127.0.0.1:8983/solr',
}
}
check init.py, base.py, sqlite.py. Then it worked perfectly.

Trouble connecting to MS SQL Server with django-mssql

I'm trying to use django-mssql to connect to MS SQL Server 2008 R2 with Django 1.4.2 These are my Database settings:
DATABASE_ENGINE = 'sqlserver_ado'
DATABASE_NAME = 'dbtest'
DATABASE_USER = 'App'
DATABASE_PASSWORD = '*********'
DATABASE_HOST = 'localhost'
DATABASE_OPTIONS = {
'provider': 'SQLNCLI10',
'extra_params': 'DataTypeCompatibility=80;MARS Connection=True;',
}
DATABASES = {
'default': {
'ENGINE': DATABASE_ENGINE,
'NAME': DATABASE_NAME,
'USER': DATABASE_USER,
'PASSWORD': DATABASE_PASSWORD,
'HOST': DATABASE_HOST,
'OPTIONS' : DATABASE_OPTIONS,
},
}
This is the error I get when I try to syncdb
Traceback (most recent call last):
File "C:\Python27\DataSatellite\manage.py", line 11, in <module>
execute_manager(settings)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 459, in execute_manager
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 232, in execute
output = self.handle(*args, **options)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 371, in handle
return self.handle_noargs(**options)
File "C:\Python27\lib\site-packages\django\core\management\commands\syncdb.py", line 57, in handle_noargs
cursor = connection.cursor()
File "C:\Python27\lib\site-packages\django\db\backends\__init__.py", line 306, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "C:\Python27\lib\site-packages\sqlserver_ado\base.py", line 193, in _cursor
self.__connect()
File "C:\Python27\lib\site-packages\sqlserver_ado\base.py", line 168, in __connect
use_transactions=self.use_transactions,
File "C:\Python27\lib\site-packages\sqlserver_ado\dbapi.py", line 151, in connect
raise OperationalError(e, "Error opening connection: " + connection_string)
sqlserver_ado.dbapi.OperationalError: (AttributeError("'module' object has no attribute 'VARIANT'",), 'Error opening connection: DATA SOURCE=localhost;Initial Catalog=dbtest;UID=App;PWD=*********;PROVIDER=SQLNCLI10;MARS Connection=True;DataTypeCompatibility=80;MARS Connection=True;')
Finished "C:\Python27\DataSatellite\manage.py syncdb" execution.
I've looked everywhere and I cannot seem to understand and fix the problem. I hope someone can help!
Thanks!
Edit:
I've already created the database. I've also connected to the database using django-pyodbc, and I've successfully read and written from the database. But django-pyodbc causes problems when I use Apache, which was why I decided to try django-mssql. However, I do not understand the error it comes up with.
My Django (1.4.2) and Python (2.7) installs run on Windows, and I'm using an Apache webserver.
Update pywin32 to build 217 or greater. I found another question on stackoverflow with the same issue (on another topic):
Python COM server throws 'module' object has no attribute 'VARIANT'

AttributeError: 'Settings' object has no attribute 'DATABASE_ENGINE'

Im new to django and python, trying to run a piece of django code on my system but im running into these problems , im running version 2.7 python and v1.4 django
$ python manage.py runserver
Running in development mode.
Running in development mode.
Running in development mode.
Running in development mode.
Validating models...
HACKUING USER MODEL
Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.commands.runserver.Command object at 0x101981e50>>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 91, in inner_run
self.validate(display_num_errors=True)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 266, in validate
num_errors = get_validation_errors(s, app)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/validation.py", line 30, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/loading.py", line 158, in get_app_errors
self._populate()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/loading.py", line 64, in _populate
self.load_app(app_name, True)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/loading.py", line 88, in load_app
models = import_module('.models', app_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Users/Kinnovate/Desktop/fsdjango/platformsite/notices/models.py", line 9, in <module>
from common.fields import PickleField
File "/Users/Kinnovate/Desktop/fsdjango/platformsite/common/fields/__init__.py", line 1, in <module>
from pickle import *
File "/Users/Kinnovate/Desktop/fsdjango/platformsite/common/fields/pickle.py", line 27, in <module>
mysql_backend = settings.DATABASE_ENGINE == 'mysql'
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/functional.py", line 186, in inner
return func(self._wrapped, *args)
AttributeError: 'Settings' object has no attribute 'DATABASE_ENGINE'
this is the part of settings.py relevant to the question
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': os.path.join(DIRNAME, 'database.sqlite3'), # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
how do i fix this?
What is /Users/Kinnovate/Desktop/fsdjango/platformsite/common/fields/pickle.py? Is it your code? Then you have an error in it, because you really don't have DATABASE_ENGINE in your seetings. Use settings.DATABASES['default']['ENGINE'] instead.
You are missing something
Because as your backtrace your database must be mysql.
File "/Users/Kinnovate/Desktop/fsdjango/platformsite/common/fields/pickle.py", line 27, in <module>
mysql_backend = settings.DATABASE_ENGINE == 'mysql'
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/functional.py", line 186, in inner
return func(self._wrapped, *args)
Because its enter in mysql_backend = settings.DATABASE_ENGINE == 'mysql' line.
As per your settings its 'ENGINE': 'django.db.backends.sqlite3', so it must be enter in sqlite please check your app because it might be possible that your settings.py might be refer from another place.

Categories