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.
Related
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.
We are a small team, trying to work with Django with a restricted access to a futurely unmanaged PostgreSQL database (i.e: only views and stored procedures ; no access to any tables) for security reasons.
We tried to give (within Postgre) the user external_test the rights to create any tables inside his own schema on external, and to use the following settings (settings.py):
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'external': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgre_db',
'USER': 'external_user',
'PASSWORD': 'password',
'HOST': 'integration.project.net',
'PORT': '5432',
'TEST': {
'NAME': 'test_db',
'USER': 'external_test',
'PASSWORD': 'password',
...
Using simple passing unit tests (project/app/tests/test_views.py):
...
class InternalTest(TestCase):
database = ['default']
def test_something(self):
pass
class StoredProcedureTest(TestCase):
databases = ['external']
def test_one_procedure(self):
with connections["external"].cursor() as cursor:
cursor.callproc("some_procedure", [42, ])
pass
...
If we try the first one with ./manage.py test app.tests.test_views.InternalTest
→ ok
If we try the other one with ./manage.py test app.tests.test_views.StoredProcedureTest
→ circular dependency issue
(ImproperlyConfigured: Circular dependency in TEST[DEPENDENCIES])
probably because it's skipping the configuration of default
If we try both tests with ./manage.py test app.tests.test_views:
→ permission denied
Creating test database for alias 'default'...
Creating test database for alias 'external'...
Got an error creating the test database: permission denied to create database
(Django try to create a database test_db as the user external_user)
We don't really get what Django is trying to do and how to properly configure it.
If we give the rights to external_user to create their own databases:
the database test_db is created by external_user
the schema of default (sqlite) is created in test_db of external (postgre)
the schema of external (postgre) is not created in test_db
Questions
Is django able to handle this ?
What are we doing wrong ?
What is the point of specifying a user external_user for TEST if in the end django is using the normal user external_user ?
Why does it write the schema of default in test_db ? Is there a way to create only models of some apps in it ?
Why isn't it able to create the schema of external in test_db ?
I hope it was described enough. Thank you in advance for your responses =)
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!
I want to set database setting in django project.
Which settings.py I should use.
I found many settings.py file.
I have setup devstack where many folders are there like horizon, cinder, nova etc.
I found settings.py in horizon folder.
and from when I setup Django I found in /usr/local/lib/python2.7/dist-packages/django/conf/project_template/project_name folder.
Please make suggest which settings.py I should use and
I can access database by user root with no database name giving in MySql Workbench.
Which database connection I should use?
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'nova',
'USER': 'root',
'PASSWORD': 'password',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
}
Please help..
You have to setup your DB in the settings.py file in horizon/openstack_dashboard folder .The DB name is devstack or any other name .All the django apps table will be created in a single DB
In my new Django project I set up a MongoDB database and use mongoengine module
but I can't properly access the dabase neither in shell nor in views.
"ConnectionError: You have not defined a default connection"
My settings.py includes the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DaTaBaSe',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '3306',
},
'tracking': {
'ENGINE': 'django.db.backends.dummy',
'NAME': 'analytics',
}
}
import mongoengine
SESSION_ENGINE = 'mongoengine.django.sessions'
mongoengine.connect(_MONGODB_NAME, 'localhost:27017')
AUTHENTICATION_BACKENDS = (
'mongoengine.django.auth.MongoEngineBackend',
)
In models.py:
import mongoengine
from mongoengine import *
from myproject.settings import _MONGODB_NAME
mongoengine.connect(_MONGODB_NAME, 'localhost:27017')
I have not tested this in-depth, but so far it worked for me:
mongoengine.connect('yourdb', alias='default')
In your settings.py file replace:
mongoengine.connect(_MONGODB_NAME, 'localhost:27017')
with the below code (notice the added 'host='):
mongoengine.connect(_MONGODB_NAME, host='localhost:27017')
To use django with MongoDB do not use the django package available on https://www.djangoproject.com and install other packages like mongoengine, if follow this process you will find lot of difficulties.
Rather you need to use the no#rel version of django that has been forked from djangoproject and added MongoDB support and I am sure it will make setup process and development process lot easier.
Follow this link to install and set up the django with MongoDB.
http://django-mongodb-engine.readthedocs.org/en/latest/topics/setup.html
One more thing you may find the error below, while setting up django.
"*Error on Django-nonrel and MongoDB: 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.*"
Follow this link to fix.
https://gist.github.com/ielshareef/2986459
Please let me know if you need any more help on this.