Do I have to use Django Models to use the API? - django

We are migrating our intranet application and have decided to choose the Django framework. We control all of our database via source control and managed scripts, so we do not use the migrate feature to create tables for us. Views and Tables can change, all business logic is held in the database.
I want to make Django API endpoint which is basically select * from my_table_or_view; and Django can return a JSON response with the column names and values. Some database tables have close to 100 columns each so I don't want to write out each and every field name and type just to return a query. What happens if we add another column to the view - will I have to update it in Django as well? What if I change the column type - will my application fail as well?
The frontend is written in VueJS - it makes a request to the API endpoint and should use the columns selected in the frontend, obviously if I remove a column it will break but I don't want to have to add the column in the django framework even if it is not used.
I've read the raw SQL queries section of the docs but i'm not sure where this applies to. Does this logic sit in the views section?
I've tried directing a URL endpoint in URLS.py to a custom class in views.py but not sure this is correct, does this logic need to be in a serializer?
I'd like the simplest method possible, potentially not using models, just raw SQL is fine.

One option for you is to sync your db to django models every time you change your schema and then use it normally, both ORM and raw SQL queries would be possible.
There's a function called inspectdb that does that natively, here is a reference to it
https://docs.djangoproject.com/en/3.1/ref/django-admin/#inspectdb

Related

How can i make models in django with form input

I Want to make a backend service website, for that I am working in django and want to make django models but with form input,
if a user create a table in my frontend I want to create a table or model in data base how can i do it?
Hello stackies ,
I Want to make a backend service website, for that I am working in django and want to make django models but with form input,
if a user create a table in my frontend I want to create a table or model in data base how can i do it?
No, you cannot create models dynamically because this would require migrations to apply the changes.
I'd recommend that you store your customer information in a structured JSON, that you can store in a PostgreSQL field, for example. Or you can jump to a document-based database, such as MongoDB.

How to Map table views of a legacy Database in Oracle to django models?

I am working on a project that requires connecting to an existing remote Oracle database. the problem is that the client has not given full access to the database. instead i'm only given accesss to some views(Relational Database View) of the database. the table names are not provided.
I'm able to perfrom raw sql queries to the legacydb in my django view.
But it would be very much more easier if i could generate the models for these views and use django ORM. is there any way to accomplish this? What I'm expecting is to use some commands like inspectdb to generate the models from the view provided by the oracle legacy database.

Django app has multiple database and multiple user

I have written one Django cloud based app. This app will have multiple user and for them multiple database, so that their data should be separate and they can save only to same database.
1) How can we implement it
2) How to automatically one user from login page to assign the database to write on it.
I don't have a complete answer, since you do not give a lot of detail. But here are a couple ots that f hinDjango supports custom database router implementations. A database router is a class that helps django decide which database to use for a particular model. Unfortunately I don't think this mechanism is granular enough for your needs. You can also specify the database to use in your code by using using(name) queryset method and save(using=name) form of save() method for instances. Of course this also means that some features of Django are going to be unvailable to you, since you cannot always expect to have a user. Look at the docs here for more info
https://docs.djangoproject.com/en/dev/topics/db/multi-db/

Django angular database form integration

I am currently trying a few ways of connecting a postgre sql database that I already have defined using a django model with a front end angular form that I created within the same project. (This a a django project using a little bit angular js)
So basically I have a form which accepts data from a user, and the data needs to be stored in one of the tables of the postgre sql database that I already created.
I was following this but there are many points that are unclear to me. Is there a better way to solve this?

Does django create a clean database?

I am building a web interface for a database at my school. The database will hold our school's versions of academic standards.
When you build a site using django, does it create a clean database? For example, wysiwyg website builders like dreamweaver create ugly html and css code on the backend. I would hate to see a similar degree of auto-generated cruft in my database.
Should I create the database myself and then build a django site to access the database, or go ahead and let django create the database?
Under any simple to moderately complex application, Django will do a fine job creating the database for you. I've yet to run into any issues with what it's made.
I would suggest that you use South to handle your table migrations. And use virtualenv and pip to set up and maintain your Django environment.
You can use the sqlall predicate of manage.py to see the exact SQL that will be executed in order to generate the database.
Obviously django needs database tables for its basic functionality (contrib.apps).
Sure, you don't have to use them, but generally you want to use a least contrib.auth and some other bundled apps:
Each of these applications makes use
of at least one database table,
though, so we need to create the
tables in the database before we can
use them.
I any case you can't and shouldn't compare it to ugly html code generated by dreamweaver or word.
On a more abstract level:
One of key concepts of a web framework (following the mvc pattern) is that you define models which are "translated" (mapped) by the framework into database tables.
A model is the single, definitive
source of data about your data. It
contains the essential fields and
behaviors of the data you’re storing.
Generally, each model maps to a single
database table.
If you want to create the whole database scheme by hand you totally missed the point of using a web framework. In most cases you simply don't need to write sql manually. You define your classes and then you can query your objects using the builtin orm.