use existing database instead of django database - python

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.

Related

Django - delete M2M field but keep join table

is it possible to remove an M2M field from a model and keep the joining table?
context:
I am trying to add through model to existing M2M field like described in this post
But doing it simply like this will result in a production app crash when accessing the old table during deployment - short window between migration and code update, when old code will try to access a new database for a few moments - without the old table in it.
You can use --fake flag when running manage.py migrate. That will make a migration file that says the model field has been removed and mark it as applied in the database migration table, but not actually execute the SQL to remove the corresponding tables etc. Read more here

Django Rebuild all migrations

I am building an app in Django and it uses a live/in-use database.
Basically since the apps development the SQL Database has undergone some structure changes and it is causing issues with Django, Django will try to apply migrations to the database that already exist. For example:
In the Django app I marked the email column as unique which was fine based on the development database. However the main database now always has a table change that marks the email column as unique. Django is fighting this unique key with the one that already exists.
So is it possible to clear all Django migrations and have it make migrations again compared to the more up to date SQL database structure?
If your models are very out of sync with your database, the easiest option is probably to rebuild your models from scratch using inspectdb.
If your models are pretty close to the database already, the first step is to make sure that your models match the database exactly. You can use sqldiff from django-extensions for this.
Once your Django models match your database, follow this answer to re-create migrations based on the existing database schema.

Django tables get generated with domain\username prefixed before table name

I am using Microsoft SQL server with Django (1.8.4). When I run migrations, the tables get created as
domain\username.table_name1
domain\username.table_name2
instead of
table_name1
table_name2
How do I resolve this? Is there a configuration for the naming format?
Django is running on Ubuntu 14.04.
This is by design, so you could have similar models in multiple applications. Otherwise, two application might use an "Expenses" or "Songs" model, and the DB will fail to create two tables with the same name.
You should usually stick with the default naming scheme.
The db_table Meta option does let you specify the table name, but it is mostly for existing legacy database.
Alter user's default schema you are connecting with. When you create a user and allow it to access a database, server create a schema of the same name and sets it as user's default schema. If you did not set one explicitly.Django does not work with schemas and does not try to create "dbo.MyTable" - it attempts to create "MyTable" and finally it goes to the default schema which is your username.

Database Tables not defined in DJANGO models

Is it fine not to define table stucture in models.py...I mean we are using
cursor.execute('some query')
so its okay not to make something like this :
def tblNew(models.Model)
col1 = models.CharField(max_length=2)
Thanks for guidance, Im a newbie in django.
You CAN create database tables directly in postgresql But you'd have to create models.py files later because otherwise you will be missing out on several things. If you have pre-existing database, then you have django-admin.py command to pick them up for you (https://docs.djangoproject.com/en/dev/ref/django-admin/#inspectdb)
ORM. It makes alot of things easyer. Writing sql in views? Why would that be good? Because that is what you get if you do not use ORM at all. And while i do have sql in some of my views, it only exists there because django lacks some features like language collation specific sorting. But otherwise writing sql in views hampers readability.
You can't use database migrations. South (http://south.aeracode.org/) is one awesome project that most big django projects use. In fact i think it was most used django project in some apps poll. Django 1.7 will provide the migrations too, but you will need to declare models im models.py for that. What you loose if you do not use database migrations - SANITY. I do not exaggerate here. Keeping track of database changes in development and prelive/live servers is nightmare otherwise.
Also is not using django ORM in some places (not declaring models) kind of strange, while you will definately use django models and ORM elsewhere - or will you stop using django authentication, sessions and site logic also? All those use django ORM...

Django lookup tables and fields

Can I look around in a database with Django? I mean outputting a list of all tables, all fields in tables and all objects in a specific table?
I know how to build models and lookup objects, but can I lookup fields in a preexisting database? I have a bit experience with unmanaged django models, but if I should use those, then I have to create models for alle the tables and specify each field. I think it should be possible to just look around in a database without 'managing it'. At least there must be a existing project on github or such with the functionality? Thank you.
You want manage.py inspectdb (https://docs.djangoproject.com/en/1.5/howto/legacy-databases/#auto-generate-the-models)
You can use raw SQL queries to execute statements like Show Table

Categories

Resources