I'm writing a Django app that uses MongoDB as it's primary database. I simply need the app to make a query against the database (hosted on Heroku) and display results for each user request.
I'm aware Python modules such as PyMongo exist for easily connecting/interacting with MongoDB, but I don't want to have to establish a database connection each time a user requests the page. I want the database base to connect upon launching the Django app.
Right now in my settings.py file I have:
DATABASES = {
'default': {
'ENGINE': 'django_mongodb_engine', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'heroku_app33177236', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
# 'USER': 'admin',
# 'PASSWORD': '',
'HOST': 'mongodb://admin:######ds041581.mongolab.com:41581/heroku_app33177236', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
#'PORT': '', # Set to empty string for default.
}
}
And in my views.py:
def index(request):
context = RequestContext(request)
# !!!!!! I want to do something like this:
rooms = db.studybug.find()
return render_to_response('studybug/index.html', rooms, context)
As you can see above, I simply want to query the database each time a user requests and display the result.
I don't really see the point or need of defining models for this, because the operation is so lightweight.
Is there a way to do something like:
from settings import db
??
Thanks!
Related
I am developing a Django application and I have created a MySQL database (I am using Laragon to manage it) and connected it with the App. I am using the database for another Python script that inserts data in the database too. What I want it to see all the database data in my Django admin site, but for some reason, I can't manage to do it? Do I have to add the tables to the Django models? or what should I do?
My settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'nlpwords',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': 3306,
}
}
I am using that database that has some data in some of the tables, but when I enter localhost/admin in my Django app I can't manage to see all those tables. Help is much appreciated.
To see your Django apps in your admin portal, you need to register them in admin.py.
Example:
# admin.py
from django.contrib import admin
from .models import *
admin.site.register(ModelName)
Note: if you just made a new app and recently created models, make sure to run makemigrations and migrate so it gets registered to the db first.
I have an existing PostgreSQL Database and I want to create APIs around it using Django Rest Framework. How do I display the tables in my Database as models in Django to further use them for the API?
First of all, you have to connect the existing DB with your Django application by following those instructions https://docs.djangoproject.com/en/3.1/ref/databases/#postgresql-notes or simply add the code below in your settings.py file
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'db_name',
'USER': 'db_user',
'PASSWORD': 'db_user_password',
'HOST': '',
'PORT': 'db_port_number',
}
}
Secondly, Django provides a powerful command which will help you out to inspect your existing DB models and save those models into your models.py file.
python manage.py inspectdb > models.py
In case that you need more information please read the official documentation https://docs.djangoproject.com/en/3.1/howto/legacy-databases/#auto-generate-the-models.
I have been working on a Django app locally and it's now time to deploy it to a staging machine.
My Postgres database on the staging machine has a different username and password to my local machine.
I have got the Django app running okay on the remote machine, except that the database has not been initialised.
I assume that I should do this with migrate, so I try running:
$ python manage.py migrate
But I see the following error:
django.db.utils.OperationalError: FATAL: no pg_hba.conf entry
for host "127.0.0.1", user "mylocalusername", database "mylocaldbname"
It's failing because it doesn't allow me to log in with mylocalusername.
I assume that mylocalusername etc must be coming from the migrations files? Certainly the local username isn't set anywhere else on the staging machine, either in my settings file, or on the actual database itself.
How can I set up this database on the staging server?
I guess one way would be to delete everything in migrations and create a new local migration. Is that what I need to do?
I thought migrations were supposed to checked into source code, though, so I'd rather not delete all of them. Also, I want to carry on working on locally and updating my staging and production machines, so I need to find a sustainable way of doing this.
"mylocalusername" comes from settings.py file.
It should looks like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'mylocaldbname', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'mylocalusername',
'PASSWORD': 'password',
'HOST': 'localhost', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
You can change it or create a valid user in your postgres database.
I'm trying to setup mongodb on my nitrousio django box. Following this tutorial.
But when I try to start my django server I'm getting the next error:
action#django-box-25197:~/workspace/django/mysite$ python manage.py runserver 0.0.0.0:3000
ImproperlyConfigured: 'django_mongodb_engine' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of: u'mysql', u'oracle', u'postgresql_psycopg2', u'sqlite3'
Error was: No module named django_mongodb_engine.base
action#django-box-25197:~/workspace/django/mysite$
I was reading about this error and I think that the Django-nonrel is not installed by default in the django's boxes and I want to know how can I use mongodb in my django box
Here is my dababase settings.py:
DATABASES = {
'default': {
'ENGINE': 'django_mongodb_engine', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': os.environ['MONGODB_DEVELOPMENT_DB'], # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': os.environ['MONGODB_DEVELOPMENT_USERNAME'],
'PASSWORD': os.environ['MONGODB_DEVELOPMENT_PASSWORD'],
'HOST': os.environ['MONGODB_DEVELOPMENT_HOST'], # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': os.environ['MONGODB_DEVELOPMENT_PORT'], # Set to empty string for default.
}}
Thanx in advance! :)
I am creating a SaaS project with Django.
I decided to use django-saas-kit for the user subscriptions and multi-accounts part.
Ideally I would like to be able to create a new site for each user and a separate database.
Does the sites framework support this? How can it be implemented?
Thanks.
You should create a "clients" folder, and a subdirectory per client. In each subdirectory, create a site_settings.py file as such:
import os.path
# import global settings
from settings import *
# this is barely just the name of the client dir, you might want to use that
SITE_NAME = __file__.split('/')[-2]
# this is the directory of the client website
CLIENT_ROOT = os.path.abspath(os.path.dirname(__file__))
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': SITE_NAME,
'USER': SITE_NAME,
'PASSWORD': 'some random password',
}
}
# you might want this too so that each client have his own MEDIA_ROOT
MEDIA_ROOT = os.path.join(CLIENT_ROOT, 'upload')
Then don't forget to use the --settings switch for management commands. For example:
./manage.py syncdb --settings=clients.demo.site_settings
Don't forget that each client will require to have his own extra stuff. For example, if you use haystack with whoosh, you need to add this so that it doesn't get mixed up between clients:
HAYSTACK_WHOOSH_PATH = os.path.join(CLIENT_ROOT, 'whoosh')
Or with django-zstask:
ZTASKD_URL = 'ipc:///tmp/%s_ztask.sock' % SITE_NAME
Or with JohnnyCache:
JOHNNY_MIDDLEWARE_KEY_PREFIX=SITE_NAME