Migrate tables in django from a read-only database - python

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

Related

How to create tables in a different schema in django?

I'm using postgresql database in my django project.
I have multiple apps in my projects.
users/
UserProfile model
myapp/
CustomModel model
Now I need UserProfile table should be created in public schema
And CustomModel table needs to be created in a separate schema called myapp
How to implement this and Do I need to change anything in the queries or migration command in future after implementing this?
Just use a meta information:
class User(models.Model)
class Meta:
db_table = 'schema"."tablename'
I've been using it from some time and found not problem so far.
More info:
This will replace table name in all your database queries with db_table. So any query will SELECT * FROM "tablename" will be converted to SELECT * FROM "geo"."tablename". Its just a neat trick, hopefully Django gives this option natively in future.

How does dependencies isolation between MongoDB and Django work?

How does Isolation dependencies between mongdb and django work?
I have made several projects with JAVA/SPRING, and the recent days,
I am studying Python django .
I successed to get connection between django and sqlite, and few days ago,
when I tried to connect to mongodb, it is hard to seperate dependencies database and django because of django-admin.
Django-admin require to attain specific Fields to each model, however, every database has each their own Field properties. Therefore, when project should change database structure, we should change a lot of code in model.py.
e.g
What if sqlite to Mongodb? only using different driver cannot make server work. e.g models.TextField(sqlite) -> models.StringField(mongodb). It is unavoidable, right? It seems difficult that solating dependencies completely between database and django
Is it Okay? do I miss something?
What if sqlite to Mongodb? only using different driver cannot make server work. e.g models.TextField(sqlite) -> models.StringField(mongodb). It is unavoidable, right? It seems difficult that solating dependencies completely between database and django
TextField and StringField isn't where you will have problems since it is supported in almost all databases, but consider this library django-mongodb-engine, it reuses the basic fields that already exist in Django ORM to the extent of what's possible, e.g.
class Post(models.Model):
title = models.CharField() <-- both Django ORM and mongodb-engine have this
text = models.TextField() <-- both Django ORM and mongodb-engine have this
This model will work as is both in SQL and MongoDB engines, but if you use a MongoDB-only (as in not in default Django ORM engine) feature like ListField()
class Post(models.Model):
title = models.CharField()
text = models.TextField()
tags = ListField() # <-- not supported everywhere
Then you migration to a SQL engine will involve some manual work to map that field.
Therefore, when project should change database structure, we should change a lot of code in models.py. Is it Okay?
Typically yes, to change the database structure you make changes in models.py. After which you would be running a migration, which is a step that generates scripts to alter the database (e.g. adding a new SQL table column). For MongoDB there's no explicit migration, as its schema is flexible (or schemaless if you prefer).
As for the admin, it's fairly smart when it comes to Django models, you may need to define the models and fields you want to appear in the admin, but in the detail view you don't have to worry about every single field, as Django admin understands them based on the information in models.py
And to change database drivers, it's the DATABASES value in settings.py

How to change Django's default Postgres database search directory

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.

How to perform a model that represents an existing table or a database view?

I'm trying to create a database view from django instead of associate a model from an existing database table. I'm new in django world and i don't know how how can i do this. Anyone have any ideia where to start look to solve this? Maybe this is not possible but can you see any alternative solution?
I understand how to define a model that as no management, by consider managed=False like i found on django docs, but how can i create an customized SQL view in my model class?
something like this:
Class myModel(models.Model):
Object = models.raw("CREATE VIEW foo AS SELECT * FROM table.A")
class Meta:
db_table = 'myview\".\"mymodeltable'
managed = False
With inspectdb management command, you can obtain the models definition from existing tables. To do that, you need to configure your settings.py file to have access to the database you want to work with and then do:
python manage.py inspectdb > models.py
You will see that it also automatically sets the managed=False. From that point, you can start querying its objects with typical objects.all(), objects.filter() and this stuff
Note: Don't forget to add the app with the imported models to the INSTALLED_APPS variable of your settings.py file.
Unfortunately maybe this is the final answer that can be found on DjangoDocs
This is useful if the model represents an existing table or a database view that has been created by some other means.

Want to add a one-to-one column to an existing table

I have a new field in the Client called note which has a one-to-one relation with Notes.
I want to be able to add the note column under the Client table in mysql. It does not work when I use python manage.py syncdb. So I would like to know how to add a one-to-one field in mysql to an existing table.
models.py
class Note(models.Model):
datetime = models.DateTimeField(default=datetime.now)
user = models.ForeignKey(User)
note = models.TextField()
def __unicode__(self):
return unicode(self.name)
class Client(models.Model):
note = models.OneToOneField(Note) # new
def __unicode__(self)
return unicode(self.user)
Syncdb doesn't change your database in order not to destroy your data. See django book.
You can update the database manually or use an automated migration tool (at your own risk) like South.
EDIT:
In django book, you can also read detailed instructions about manual changes along with SQL examples. Other useful information can be found in MySQL manual.
You didn't say what the application name is so I'll assume it is fooapp.
First look at the SQL Django would have used to create the relevant tables:
python manage.py sql fooapp
For additional fields run this sql command:
ALTER TABLE fooapp_client ADD ...
where ... is the field declaration you saw in the output of "manage.py sql fooapp"
Remember to also execute any index/constraint commands in the "manage.py sql fooapp" output that operate on the new field.

Categories

Resources