I used Django REST framework to write API, the Django ORM is really conveinent like DB migrate or relation field's query.
But now we have a case, there is a DB with about 30 tables with data in it. And we have to query the DB and write some API. So I think I can't use Django this time.
What develop tools do you recommand to write restful API in Python if your case have the database that already created tables in it?
You can use inspectdb command to create django models from existing database.
python manage.py inspectdb > models.py
Here is the docs: Integrating Django with a legacy database
Related
I am new to Django and have a question in regards to whether or not I have to re-enter data or can use existing data from a postgres database and apologize if this has been asked before.
I have a Postgres database in which I created with SQLAlchemy ORM. I use this database for data analysis and want to create a web app for the data presentation. I know that Django has its own modeling system and ORM, do I have to recreate the database from scratch since I created this with SQLAlchemy's ORM?
You can use inspectdb command from Django.
https://docs.djangoproject.com/en/4.0/howto/legacy-databases/
I already have my flask-admin app with its models and database and tables created..
Now I would like to add a field to model and that should be reflected in the database by a new column in the model's corresponding table.
With django one would create then apply migrations..
How do I make migrations/schema-changes in flask-admin?
I would recommend checking out the section on SQLAlchemy-based database migrations in Miguel Grinberg's Database Flask Mega-Tutorial. He uses low-level SQLAlchemy APIs to put the database under version control and automate the process of database version upgrades or downgrades.
Alternatively, I recommend using Miguel Grinberg's Flask-Migrate extension which implements the functionality he describes in the aforementioned tutorial. Using this extension, you simply add an instance of the Migrate object to your application, then perform database migrations using a command line interface provided by the extension.
How I can select data from a database that is not inside my django app?
How I should connect my django query to a database that has not been created by my django app?
I have put this code in views.py:
def select(request):
p = user.objects.filter(country='Iran')
return HttpResponse(p)
If the database had been created by django app then there was need to add this part into views.py:
(for example)
from users.models import user
But here what should I do?
P.S:
I'm using pymysl in my django project and have installed lamp in my linux os, and using phpmyadmin. Any more explanation is needed?
Django supports several databases in one instance.
Also you can manually connect to DB in your python code.
For example:
import psycopg2
psycopg2.connect(...)
According to the comment you made on the other answer,
First version of our project is in php, we want to migrate to python-django and use the same old database. So we can't create databases again and we should configure our django project to work with existing db.
You want to use an already existing legacy database in django. Django offers the possibility to create models out of your old db (legacy-databases).
Configure the db settings of your old database. Then you can run inspectdb to create django models of your old database:
python manage.py inspectdb > models.py
I am trying to access the database in Booktype. I am using sqlite3. I am not able to view the tables. And even don't know how to create a new model.
How do I access and add model to the booktype?
you could use sqliteman to view your sqlite3 database
you should learn basic django docs, models
when run python manage.py syncdb, django will create tables in database (configuration in settings.py)
I've written some custom PostgreSQL views, which I've wrapped with Django's ORM using the managed=False meta flag. What's the best way to deploy these views, so that running syncdb updates them in the database, and also that the views exist in the test database when I run unittests?
With an initial SQL file.