How to change model and update database, make migrations, in flask-admin? - python

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.

Related

Call Postgresql procedure from Django

I am using Postgresql in Django and Python,and I do all the CRUD operation successfully but I don't know how can I call Postgresql procedure from Django and show the result in my web application!!
This should be completed using Django's ORM (Object Relational Mapper) querysets which is a Django wrapper that adds a layer of abstraction above an SQL query that retreives a given dataset from PostgreSQL (or another supported database).
Indeed, the existence of this layer is in place for security reasons in addition to enabling a developer to use any of the supported underlying databases (SQLite3, PostgreSQl, MySQL, Oracle or Maria DB (Django 3.0+)) by simply switching out the database credentials and settings.
The rendering of the datasets will be completed with either Django's templating system if you're usind Django across the entire stack. It could also be completed by creating APIs and using a front-end framework to render from them.
You should Google Django ORM and Datasets for further information, documentation and tutorials to achieve this.

Can I use an external database table for the login process in Django?

So I'm starting a new Django project that essentially requires the login & registration process be routed through an EXTERNAL & ALREADY created database.
Is it possible to have the User model use an EXTERNAL database table ONLY when Django is:
Logging in a user, to check if the login is valid
Registering a user, inserting data for that user in the external database
I would like for the rest of the Django server to use a local database.
If so, could someone either provide examples or guide me to documentation on the subject?
Easiest way to use multiple database with Django is to use a database routing. By default Django stick to single database, however, if you want to implement more interesting database routing system, you can define and install your own database routers.
Database routers are installed using the DATABASE_ROUTERS setting. You have to specify this setting in your settings.py file
What you have to do is write one AuthRouter as described Django documentation Django Multiple Database
"Yes, but"
What you are looking for in the docs is called "database router".
There is even an example for the auth app in the docs there.
But, there is s serious drawback to consider with this approach:
We cannot have cross-database relationships in the models. If auth tables are in a separate database, this means that any otehr app that needs a foreign key to User model is going to run into problems. You might be able to "fake" the relationships using a db that doesn't enforce relationship checks (SQLite or MyISAM/MySQL).
Out of the box, such apps are: session, authtoken, and admin (and probably more).
Alternatively, a single-sign-on solution might do a better job: django-sso, or django-mama-cas + django-cas-ng, or the commercial Stormpath.

Python restful API tools selection when DB tables already created

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

To add model and access database in Booktype

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)

Deploying Custom SQL Views with Django

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.

Categories

Resources