I Want to add Redis Database in Django-1.9, so i followed this documents for integration
https://niwinz.github.io/django-redis/latest/
But i didn't find any clue on how to mention Database name in the settings, Here i want to mention Redis as a database on behalf Sqlite3, If uncommented this line django is throwing an Error of DATABASE not found
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'sqlite3'),
}
}
Thanks in Advance for your solution
What's django-redis?
django-redis is a BSD Licensed, full featured Redis cache/session
backend for Django.
What's redis
Redis is an open source (BSD licensed), in-memory data structure
store, used as a database, cache and message broker
Essentially that means django-redis is a django package that allows you to replace the default memcache as django's cache backend and also allows you to replace the DB as the default session storage. However django-redis does not implement features required to use it as a replacement for sqlite3 or any other database.
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/0",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient"
}
}
}
To use the redis database in django you need to add this code to your settings file,based on your requirement you can change the value of the database at the end of LOCATION value like ("redis://127.0.0.1:6379/1") for database '1'.
You can also check here: https://niwinz.github.io/django-redis/latest/#_configure_as_cache_backend
By default Django provides no support for non-relational database backends. However, if you intend to use Redis as your primary database, you can take a look at Django non-rel.
Related
I am building an app using Django and Postgres. I managed to do migrations and I want to test it. When I test with sqlite everything works fine, but when I run tests with postgres I'm getting this error:
Creating test database for alias 'default'...
Got an error creating the test database: permission denied to create database
I've checked user's permissions and I'm sure that this user have permission to create database.
My database config looks like this:
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
# }
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '***',
'USER': '***',
'PASSWORD': '***',
'HOST': '****',
'PORT': '****',
}
}
My postgres db is on a server.
My questions are:
What is the right way to config my db and run tests?
Should I be using sqlite for testing?
If so how my code should look like, so I don't have to comment configs?
It looks like your DB user doesn't have permission to create a new database. Please, take a look here. This command-line utility allows you to create a user and set their permissions.
Example:
createuser my_user --createdb -W --username postgres
Note: you are creating user "my_user" on behalf of PostgreSQL admin role which is postgres by default.
Answering your questions:
You may have several configs for different stages, e.g development, testing, production.
You could use both SQLite and Postgres databases for testing purposes to some extent. You should be awarded, though, if your app relies on some specific features available only in Postgres, then using SQLite for testing doesn't make sense. I personally prefer using the same database for all stages. You could also use docker if you don't want to install DB server on your machine.
I have an API running on Heroku and would like to be able to test it using the test database. My problem I have is that the TestCase setUp(self) method adds the data to an automatically created test database. Then my tests send a POST request to the locally running version of itself. That code then is just using the regular default database instead of the test database the tests are running in.
Here's some code and what I've tried.
In my main settings.py I have named my database like so
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'theOneTrueDB',
}
}
And I read here (https://docs.djangoproject.com/en/3.0/topics/testing/overview/#the-test-database) that
The default test database names are created by prepending test_ to the value of each NAME in DATABASES.
So I figured in my views file for one of my apps I could just do
Tenant.objects.using('test_theOneTrueDB').all() and that would work but it says
django.db.utils.ConnectionDoesNotExist: The connection test_theOneTrueDB doesn't exist
I have also tried
Tenant.objects.using('test_default')
and many other things. I am lost. I would also be okay with setting up another database and then using that in the setup with
Tenant.objects.save(using='theOneTrueDBTest)
or something like that.
Any help would be appreciated!
EDIT:
I know have my settings looking like this
DATABASES = {
'default': {
'NAME': 'theOneTrueDB',
'ENGINE': 'django.db.backends.sqlite3',
},
'other': {
'NAME': 'theOneTrueTest',
'ENGINE': 'django.db.backends.sqlite3',
}
}
but then if I try to save to the other database like this
tempPM.save(using='other')
Then I get this error
AssertionError: Database queries to 'other' are not allowed in this test. Add 'other' to API.tests.TenantLogin.databases to ensure proper test isolation and silence this failure.
You don't need to access the test database directly. It will be used by default and you don't need to worry about it.
By default for sqlite3 backend will be used in-memory database, with the name like this file:memorydb_default?mode=memory&cache=shared,
you can examine the settings while tests are running via:
from django.conf import settings
print(settings.DATABASES)
I'm a new developer for AWS redshift.
As far as, I knew AWS redshift is pretty similar to postgresql
Is there any library or module for manage(like auto-migration, auto migrate) between redshift and Django?
I did create a table and access data by using SQLAlchemy and redshift but I don't know how to do that thing in python Django.
any suggestion or idea is okay!!
I got this error with this command
python manage.py migrate
django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (Column "django_migrations.id" has unsupported type "serial".
You could easily connect your RedShift services with your Django App.
Try to add the code below to your settings.py file
DATABASES = {
'default': {
'NAME': '[Your Cluster Name]',
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'USER': '[Username ]',
'PASSWORD': '[Password]',
'HOST': '[Pathname]',
'PORT': 5439,
},
}
But, you need to know that Redshift doesn't enforce primary keys. In my opinion is not a great idea to use Redshift as your default DB in your Django Application as Redshift is a helpful DB for a data warehouse and not for an app.
To sump up, my suggestion is PostgreSQL.
I googgled alot and see many solutions, but none of them worked for me. I have a project in django and also an app as well. now, I want a connection to mysql database that reside in wamp server in order to interact (sending data or getting data) to database through my app.I also installed MYSQLDB for python. and I have done neccessary things in settings.py file.following is content of settings.py regradding of database
DATABASES = {
'default': {
#'ENGINE': 'django.db.backends.sqlite3',
'ENGINE': 'django.db.backends.mysql',
'NAME': 'flask',
'USERNAME':'root',
'PASSWORD':'',
'HOST':'127.0.0.1',
'PORT':'',
}
}
In this link, you can find information about Django setup for WAMP. Also you may need to add port number to the database settings which is 3306 as default.
In this tutorial, it never says to edit the settings.py, in the previous docs, which supported django 1.5 you needed to edit settings.py.
So do you need to edit that file or not? Did the author skip that part because it was kinda obvious?
I actually don't see the name settings.py in the new docs, and there aren't too many mongo db django tutorials on the web. And the questions here, if any are outdated. Thereby I'm sorry if this turns out to be a naive question.
And if you want to use pymongo, AFAIK you don't connect from settings.py so I just had to ask.
Personally, I like to have all my database configs in settings, so I have mongo database configuration in settings.py along with my relational database configs:
MONGO_DBS = {
'default': {
'alias': 'default', # the alias that Documents refer to
'name': 'default', # the name of the database to connect to
'host': 'localhost', # the host
'port': 27017, # the port
'username': '', # not implemented
'password': '', # not implemented
'enabled': False, # whether or not we connect to this database
},
}
Then, I have a little snippet of code that runs in settings.py (cue some grumbling) and connects to all of the relevant mongo instances:
from mongoengine import connect
import sys
if not (len(sys.argv) > 1 and sys.argv[1] == 'test'):
# Don't run this if we're running in unit tests. The test runner will spin
# up the appropriate databases and spin them down appropriately.
for db_name in MONGO_DBS:
db_meta = MONGO_DBS[db_name]
if db_meta['enabled'] and 'alias' in db_meta:
connect(db_meta['name'], alias=db_meta['alias'], host=db_meta['host'], port=db_meta['port'],
lazy_connect=db_meta.get('lazy', True))
Obviously, this code is still somewhat incomplete in so far as authentication doesn't happen. But it should be a reasonable launching point for you.
I should add that I just dug up references to settings.py in the django documentation page for mongoengine. Currently, it's located at http://docs.mongoengine.org/en/latest/django.html.
Lastly, I should add that this advice applies through mongoengine 0.8.7 (latest as of this answer). YMMV with future versions.