How to create tables in a different schema in django? - python

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.

Related

Migrate tables in django from a read-only database

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

Django with postgresql db, query with ONLY keyword to look only parent table?

I have a table my django model is pointing to, db side i set a series of triggers to route information in child tables (by table inheritance) for a rotation system. Querying the parent table i can still have my full set of information even if dislocated in many tables,but to improve my performance i want to search only in parent table, corresponding with a query where i specify "ONLY" to make db not search in child tables. Is there a way to do it with django models?
Posible duplicated question
You can create a view in postgres:
CREATE OR REPLACE VIEW my_view AS
SELECT * FROM ONLY MY_TABLE;
Create a model in django pointing to that view:
class MyModel(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=100)
class Meta:
managed = False
db_table = 'my_view'
Then query that model.
Or you can use directly .raw()

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.

Categories

Resources