One single MySQL database for many web2py applications - python

I have 3 apps under my web2py framework. Can I use one single MySQL database for all of them (by prefixing tables) so users just need only one single registration to access those 3 apps? I fail on my attempts. Or any other solutions? Thanks so much.

If multiple apps share the same database table, all but one of the apps must define that table with migrate=False to prevent migration attempts. Note, Auth.define_tables() also takes a migrate argument, so to turn off migrations specifically for the Auth tables, you can do:
auth.define_tables(migrate=False)
You can also turn off migrations for an entire database connection within an application with:
db = DAL(..., migrate_enabled=False)
See here for more about inter-app cooperation.

Related

django rest framework apis using legacy db without creating django default tables

I am going to develop django rest framework apis using postgres legacy database in which apart from the existing tables no other default django tables should be created. I would like to know without creating any django default tables or doing migrations,
can i able to access records from tables using ORM?
Can i able to access admin page without creating django auth table (i.e superuser)?
If no for both questions, only way to connect to db is using any db adapter like psycopg2?
can i able to access records from tables using ORM?
If the schema is same in your models and DB tables, then yes.
Can i able to access admin page without creating django auth table
(i.e superuser)?
As far as I can tell, no, you can't get that, unless your existing 'tables' already have that, and you have all the necessary credentials.
For question in #3, I'd use something less constraining like Flask if you have these problems.

Django not using databases from settings.py

I'm a few weeks into Python/Django and encountering an annoying problem. I have some existing databases set up in settings.py, everything looks good, I've even accessed the databases using connections[].cursor()
But the databases (and data) are not making their way into models that I want to use, despite doing the makemigrations and migrate commands. I was able to use py manage.py inspectdb --database-dbname and copied that class information manually into my models.py, but that didn't work either (typing py manage.py inspectdb on its own does not pull up these databases, I was only able to view by that --database extension). So I'm stumped, as it seems I'm doing all the right steps but not able to use these existing databases in Django.
Any other hints and steps I can take are welcome!
(Almost) all the tutorials, examples, and third-party app you'll find on the internet, and most of the Django documentation assume you use one database for your app. That's because it's fairly tricky and unusual to use multiple databases in one app.
But it's not impossible to use multiple databases and the documentation contains instructions on how to do this and what changes you'll need to make to make it work.
IMO, these are the pre-conditions to use multiple databases in one project:
The databases contain explicitly unrelated information, i.e. you won't have SQL relationships between tables in different databases. One database may contain a table with a column that maps to a column in a table in another database, but they aren't explicit (no ForeignKey or ManyToManyField in your models).
You don't need to mix databases in one query: This basically derives from the previous condition. It just means that if you need to get objects from one database that depend on the rows coming from another database, you establish the relationship in python. E.g. fetching as list of names from one database and using that list to filter a queryset on the other database.
For example, if you have an existing database that contains Strava routes (which are regularly updated via some external mechanism) and your app is a broader app that helps users getting to know their neighbourhood where they can recommend locations and things to do, being able to offer a list of routes with a starting point nearby might be something you'd want to show.
Now that you know this, the way to go is described in the doc linked above:
Create a database router so that queries for certain models are automatically routed to the correct database. E.g. Route.objects.filter(start_city=city) would automatically fetch routes from your Strava routes database.
If you need to save information about a route in your app, save it in a model in the default database and use a unique identifier of the route that will map to the strava database. Use separate queries (no relationships) to fetch information about a specific route.
That being said, if the Strava database is not regularly updated via 3rd channels and its purpose is just to pre-populate your default database, then export the data from the Strava database as json and import it into your django db using manage.py loaddata or a migration file, the latter being more flexible as to the structure of the json file.

Does Django multiple database support generate tables for only some specific apps in a database

I aim to store history of tables using django-simple-history library in a separated database from the main one. When I run migrate --database=the_second_db, Django generates all tables for all apps in the second database.
How can I make it just generate some specific tables for specific apps that I need in the second database( history tables)?
Yes, this is possible by using Django Database Router, also you must define multiple database configuration on the DATABASES constant on the your project settings.py file.

Adding client field in django ORM

I have an existing app written using django framework.
This app is using django-registration-redux to identify the users.The app is using ORM to work with db.
I have to share the app for more then 1 client.(to make it SaaS)
I am adding a client field in every table and want to have every user assigned to certain only one client.So even if same DB is used different users that belong to 2 different clients will have totally encapsulated data set from each other.
Is there any existing package that can do it?
Try django-tenant-schemas.
It should solve your problem.

Django-guardian on DB with shared (non-exclusive) access

I am developing a Django app being a Web frontend to some Oracle database with another local DB keeping app's data such as Guardian permissions. The problem is that it can be modified from different places that I don't have control of.
Let's say we have 3 models: User, Thesis and UserThesis.
UserThesis - a table specifying relationship between Thesis and User (User being co-author of Thesis)
Scenario:
User is removed as an author of Thesis by removing entry in UserThesis table by some other app.
User tries to modify Thesis using our Django app. And he succeeds, because Guardian and Django do not know about change in UserThesis.
I thought about some solutions:
Having some cron job look for changes in UserThesis by checking the modification date of entry. Easy to check for additions, removals would require looking on all relationships again.
Modifying Oracle DB schema to add Guardian DB tables and creating triggers on UserThesis table. I wouldn't like to do this, because of Oracle DB being shared among number of different apps.
Manually checking for relationship in views and templates (heavier load on Oracle).
Which one is the best? Any other ideas?
I decided to go with manually checking the permissions, caching it whenever I can. I ended up with get_perms_from_cache(self, user) model method which helps me a lot.

Categories