I have a Postgres Database and it has plenty of tables in it, using Django I need to query or get all the table rows into my views.py. After reading I understood that creating a model in models.py will create a new table into my DB which is not what I need.
Is using django a good way to fetch data from postgres or should I use the python library to communicate with postgres directly (psycopg2)?
If django has a way to fetch data from table then how do I do it?
You can inspect the existing database schema by
python manage.py inspectdb
for adding the database model to models.py
python manage.py inspectdb > models.py
Related
i want to connect my existing database of sql server with django but the problem is django has its model which create its own database but i dont want to create database using django i just want to use mine to retrieve data.
the one solution i saw was to use inspectdb but hte problem with inspectdb is that it does not pick wring keys and constraints sometime plus have to set many thing manually but in my project my database is user defined user will connect its database so i dont actually know that how many and what table user's database have do i just want to connect that database with django and use the value of it.
my existing database is sqlserver.
any technique to use my existing database without using django database and retrive data from existing database.
thankyou.
As you mentionned, you should use : inspectdb to create your models as stated in the doc (https://docs.djangoproject.com/en/4.0/howto/legacy-databases/)
Setting the managed option to False in the Meta class of model, you can instruct Django to not make any migrations or database schema modification, but you need to tweak the model yourself to ensure every kind of mapping is as you intendend, and obviousl ensure coherence between your DB schema and models.
Is there a way to migrate all tables from an oracle read-only database to django?
So basically I don't want to make any modification to my database. I just want to extract information from it.
From what I found till now, is a way by using routers but I don't know how exactly to use it.
Thanks, any help will be appreciated
DB: oracle
Django version: 2.2.12
python: 3.6
cx-Oracle: 7.3.0
you should use model.Meta.managed=False
you will define a normal model and set this attribute in it's meta
class AModel(models.Model):
field1 = ...
field2 = ...
class Meta:
managed = False
db_table = "name_of_already_created_table"
if by router you mean Database routers it should only be used if you have more than one database to work with.
you should write your models based on the tables and columns that you have in your oracle db and add the managed=False to each of them separately.
so if you have for example three tables in your database you will write three separate models for it in your models.py file. this way django wont create the tables and just trys to read those tables also you need the db_table attribute and value of it should be the name of your table in the database
I am new to Django.
I created the sqlite3 database called "test.db" and I'm not using the models.py or default database to sync in views.py for saving data.
I've created the HTML to get the value to store in test.db and print the data using the normal sqlite3 query.
The problem is that I have to access the test.db database in admin panel to search and print the data stored in the database. How to do this task?
You'll need to set up models.py for you to see data in admin functionality of the site. This is because the register statement register(*models, site=django.admin.sites.site) uses the models to display the data. If you are working on legacy database which you aren't, in this case, try this
I am creating a Django web application and I am using Postgres for my database.
Under my project, I have a web application named 'home', and I created a table named 'myTable' in Postgres.
Whenever I try to save something in the table, Django automatically looks for the table called 'home_myTable' instead of 'myTable'. For example, if I do
python manage.py migrate
I get the following error:
django.db.utils.ProgrammingError: relation "home_myTable" does not exist
I have been working around this by manually giving Postgres commands using Psycopg2, but this is really annoying.
Is there a way to stop Django from automatically start looking for 'home_myTable' and instead make it search the table that I want?
You can set the db_table Meta option for you model.
class MyTable(models.Model):
...
class Meta:
db_table = 'mytable'
Unless you are dealing with a legacy database, I recommend that you define your models, create migrations, then let Django create the tables, instead of creating the tables manually as you are doing.
I have a file - "codemets.db", which contains a lot of tables and data for my project. Some of the tables are:
- auth_group
- auth_group_permissions
- auth_user
- auth_user_groups
- ...
- mappingapp_coordinates
- ...
I am using PyCharm to create a new app on Django. I am trying to use the already existing database "codemets.db" and I am trying to auto-generate the tables into models. I am using the following commands:
python manage.py syncdb
python manage.py inspectdb > models.py
python manage.py makemigrations
python manage.py migrate
However, I am getting all of the models for the tables only until the mappingapp_coordinates.
For mappingapp_coordinates and the others after that, there are no models. Can someone help me with that? How can I generate all of my tables into models?
My second problem is:
when I open the app on the admin page I can see the tables (only the tables that have been generated into models), but there is no content from the database in them (they are empty). How can I put the data from my database into my app?
Thank you in advance.