I want to develop a website (with it's own database) where all the users will host a tiny database on their computer (SQlite or mySQl). I need to find a way for the users to access their local database and work with it on the website.
I read the docs concerning the multi-database (https://docs.djangoproject.com/en/dev/topics/db/multi-db/) we can in the settings tell to django which database it needs to connect to.
For example :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
How to tell django to connect to the database hosted by the user ? I assume that 127.0.0.1 will refer to localhost of the django server. Am i right ?
I think you misunderstood the concepts behing Django, or backend at all.
Django runs on a host, it could be a server, cloud hosting, on your local machine, etc.. When you use 127.0.0.1 as the database server host address, you are targeting localhost of the host where Django is running. You cannot simply start a MySQL server on the client side from django or python, you cannot do anything. You cannot even start a SQLite database as the django simply has no ability to see to user's filesystem.
What exactly are you trying to achieve? If you want to build something on top of a local database, it has to run locally, or you will have to somehow mount the database files to the django, which will be a snapshot instead of realtime change relfection.
Related
guys. I have a database, which I deployed on Heroku.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'd7mj3h2mco40v9',
'USER': 'fwla1qyxgxrrqk',
'PASSWORD': get_secret('DB_PASSWORD'),
'HOST': 'ec2-54-174-73-136.compute-1.amazonaws.com',
'PORT': '5432',
}
}
The problem is when I add comments objects on a local machine, they automatically downloaded to the remote database. It's not convenient.
I want to create, for example, a local database which will save all migrations or changes of my models, but not downloaded into a remote server. And when I need to download the data, I will do something and the changes are to appear on a remote database.
What do I need to do for this? How to make the right migrations and then work with them?
I'm newbie, please don't advice complicated things I can't understand :)
Not really a newbie topic but you could look into db routers https://docs.djangoproject.com/en/4.0/topics/db/multi-db/
Simplier would be the solution detailed here https://zerotobyte.com/django-multiple-databases-setup/ as you can see you just have to add db in the settings then make migrations by specifying the chosen db.Good luck
I have a running and working database in SQLite but multiple users are writing to it in the same time and i seem to have concurrency problems, because of the database being locked.
What do i need in order to create and access a PostgreSQL database in Python? Is there a way in which i dont install anything else than python libraries? I have a company laptop and would need 1XXXX approvals to install a third party software.
The setup is simple: The users go to the upload page on the site(built in django), uploads a xls file and the info gets extracted into a database(while being compared with info from inside the database). If 2 users try to upload files in the same time, i get database locked error.
Any help would be great, either to solve this SQLite problem or to get me started in Postgre if this should solve it. THX!
SQLite is not suitable for multiple user access since it is just a file acting as a database. To get the real power of DBMS you have to switch either MySQL or PostgreSQL which are officially supported by Django. Remember that PostgreSQL is preferred by Django for production use. Anyway you can use Oracle DB if you company already have it.
You need to setup settings.py in your project folder in order connect any database with Django as follows.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': '',
'PORT': '',
}
}
You may need to install relevant db driver to connect it with Django.
Before committing to migrating to PostgreSQL, you may want to simply increase the busy timeout of your SQLite3 database and see if that produces acceptable results.
I am new to Django; intermediate in Python3.6. Now, I've created a basic Django app which displays index.html. Now, I want to create a very simple form in index.html which will query the database and print the results in the same form. I already have the postgresql database in my local with all the data needed; it is very simple with just one table (right now, my Django app is connected to the default sqlit3 db). Where do I go about creating the query to fetch the data and populate it?
You need to install these libraries first :
sudo apt-get install libpq-dev postgresql postgresql-contrib
Configuration
in settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'db_name',
'USER': 'db_user',
'PASSWORD': 'db_user_password',
'HOST': '',
'PORT': 'db_port_number',
}
}
You can refer to these two links:
For More Detailed information about setup :Tutorial for setting up application using PostgreSql
Documentation link for writing Queries
Glad you're coming on board with Django! It's a great language with plenty of useful features, you won't regret learning it.
To connect to an existing database, check out the docs here. This explains how to use your existing db to create models in your project.
I also recommend checking out the tutorials in the docs starting here. This tutorial will explain how to connect to your postgres db, as well as how to begin writing queries for your views. This is the place to start learning the basics of Django, just follow it along and you will get the hang of everything in no time
I am trying to use ElasticSearch as a NoSQL database in my Django project.
The goal is to plug Kibana on the other side and be able to visualize my data there.
I do not have a choice of adding another database and using ElasticSearch only for indexing as my project is plugging to an existing infrastructure.
So I have installed the following two modules:
django-haystack
elasticsearch
I was expecting to not need SQLite (or any other database) for storage and to use ElasticSearch as a NoSQL storage (is that wrong?)
I added the Haystack connections settings to my project settings and then, looked for getting rid of "DATABASES" or replacing it with values pointing to my ElasticSearch with no success :(
Here is what I'm stuck with:
settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': 'http://127.0.0.1:9200/',
'INDEX_NAME': 'haystack'
},
}
On all the answers I could find on StackOverflow and code snippets I would never see these DATABASES settings changed, so I assume people were using a database for storage and ElasticSearch only for their search engine/indexing which is not what I am looking for.
Is there a way I can have ElasticSearch as my storage DB (just like I would with MongoDB), not breaking the Models and interface of Django?
It's not possbile (officially) to use ElasticSearch as your django database backend (I assume that this is what you want) - currently supported backends are:
postgresql
mysql
sqlite3
oracle
The above is official list, unofficial (3rd parties) is as follows:
SAP SQL
IBM DB2
Microsoft SQL
Firebird
ODBC
Anyway, the people around django are very nice and hard working developers :) And there is package for that (you can give it a try - never used it before):
https://github.com/aparo/django-elasticsearch
One more thing - why do you need django? Is kibana not enough? You gonna make some changes in data on the ES index in your Django App? Or should it be just readonly?
Because if you want use the ES like a normal DB storage - it won't work, as you will wait each time you update/insert data about 1-2 seconds till ES index new data. This is just not the purpose of the ES.
Hope this help, happy coding.
Can django use an external database? I mean, if you have one server for the db and other n-servers with the web server, can django use the db on an external machine? Can django do queries via internet to another django db?
Yes, your database and web server can be on separate servers. You just have to specify in your settings file the host. See https://docs.djangoproject.com/en/dev/ref/settings/#databases
As the relevant documentation states, Django is capable of using multiple databases. Whether remote access is supported will depend on which one you choose to use - but, as a general rule, it is supported, with the notable exception of sqlite
Yes you can access the database from anywhere. But they(you) need to provide database privileges for your IP. some code is like,
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': Remote Host,
'PORT': '5432',
}
}
You can also use external django package https://github.com/kennethreitz/dj-database-url for deployment.
I reach this post, but in my case, I need to access another database. My intention is create an application that report informationa from another database. I needed to use two databases. I found the way in the django documentation.
https://docs.djangoproject.com/en/3.2/topics/db/multi-db/