Django - Migrating data inside tables for smooth release - django

I am using Django Rest Framework for backend development, where I have set up 2 environments. I have split settings files as required and both the environments have different DBs which are working fine, I am using Zappa(AWS lambda) for deploys, I was wondering is there an elegant way of migrating data(not models but data inside models say a few config changes) than preparing PostgreSQL DB rollouts, Could not find much on the internet, so asking here.
Please let me know if there are any questions.

Related

Two flask apps (models) using same table

I build 2 flask Apps App1 and App2(Two different services). Both Apps are referring to the same DB. Am using MongoDB as database and MongoEngine to create connections and to support ORM Queries.
I have created a user table in App1 and I defined the structure of the table in models.py file. Now I have to use the same user table in the App2. How would I use the existing table itself without rewriting the same code in APP2?
I can do it in one way that I can write a Mongo wrapper which will connect and serve the data. But I don't want to write RAW queries. Can someone help me how to do this? Thanks!
You can put all your database-related code into a separate Python package which both your applications can then import.
OR
You could also consider building a separate application around your database code that exposes information through an API. Your other applications could then make requests to this API.

DJango: Multiple instances of an application in a single project

I have a django application and i've needed to create multiple instances. So currently i create a virtualenv for each instance with its own django project. However this is not scaling well. What i really need is multiple instances of the application inside a single django project.
Is there any sort of examples or advice for something like this? I was thinking of using multiple databases, but then each db would get all of the models for all applications in the project.
I think you would like to see Multi Tenant Applications in Django.
If you follow this book it contain what you are looking for. Building Multi Tenant Applications with Django
Taken from the above shared docs.(Summary)
The various approached to multi tenancy
Shared database with shared schema
Shared database with isolated schema
Isolated database with a shared app server
Completely isolated tenants using Docker

In Django, how do I make a model that queries my legacy SQL Server and Oracle databases and sends that to the view?

I have a lot of old vbscript webpages using Classis ASP. Those ASP pages have lots of different databases queries to different databases all inside a given .asp file. I want to write those in Python and use Django as the framework. Will I be able to do this? I'm not sure how to start after I install Django. Sure, I can make a demo work, but that's not what I'm after. I will use the normal database "things" in MySQL or PostgreSQL, but sooner or later I have to hit those other databases and bring them back into Django, using Django's templating and so on.
Will I be able to do this? How do I make a model that queries my legacy SQL Server and Oracle database and send that to the view? Am I "fighting the framework" to accomplish this?
Just to be clear. I am not interested in messing with the stock database that Django uses for it's settings. That can stay as it is. I want to use that part for plugins, security (ldap), etc.
Thanks.
You can query different databases in your ORM calls by leveraging the using statement: https://docs.djangoproject.com/en/1.5/ref/models/querysets/#using
This would allow you to set up as many database definitions as you need in settings.py, then specify which DB to query at the view level. That way, you wouldn't have to change your model definition should you decide to consolidate your databases, etc.
Have you reviewed the Django multiple databases documentation?
Django has a built-in Oracle back end, so that should be fairly straightforward.
SQL Server can work through django-pyodbc but I found it fairly painful to set up. If you already have a generally working ODBC connection to your legacy SQL Server database connection from your Django environment it's no big deal, but it took me some trouble to get things set up to where I could use tsql to connect to my SQL Server database.
I also had some trouble with Unicode data from the SQL Server database until I forced it to use the appropriate (later) version of TDS. I just needed it for a script or two, so I set the 'TDSVER' environment variable and left it at that, but in theory there are other places you can set that.

How to migrate Django project to Google App Engine

I am looking for a guide to migrate Django project to Google App Engine and use Google's datastore. The most of the guides I found were linked to Django-Appengine using Django-nonrel (but I want to use GAE's native support).
Going through GAE getting started guide, it says:
Google App Engine supports any framework written in pure Python that speaks CGI (and any WSGI-compliant framework using a CGI adaptor), including Django, CherryPy, Pylons, web.py, and web2py. You can bundle a framework of your choosing with your application code by copying its code into your application directory.
I understand that I won't be able to use some features of Django in that case (majorly the admin feature) and would also need to restructure the models.
From other reading, I also found that latest SDK of GAE now includes Django 1.3 on Python 2.5.
I tried to put all files from my Django application to a GAE project, but couldn't get it all to work together.
Please provide some basic guide using which I may migrate my Django project to Google App Engine's code.
Thanks.
For an existing Django app, using django-nonrel is the simplest approach; it is very popular so you should be able to find help with specific errors you get quickly.
Another approach is written up in this article: http://code.google.com/appengine/articles/pure_django.html -- it goes the other way, taking an App Engine app that uses Django for dispatch, templates, and forms, but not for models, and describes how to make it run in a native Django environment. Maybe you can glean some useful hints for your situation from it.
I've used django-nonrel, which behaves pretty much like django, except that operations with JOINs will return errors. I've basically worked around this by avoiding ManyToMany fields, and essentially building that functionality manually with an intermediate table.
So far I've ran into two problems with Django-nonrel:
1. No access to ancestor queries, which can be run in a transaction. There's a pending pull request for this feature though.
2. You can't specify fields that are not indexed. This could significantly increase your write costs. I have an idea to fix this, but I haven't done so yet.
(Edit: You CAN specify fields that are not indexed, and I've verified this works well).
2 (new). Google is pushing a new database backend called ndb that does automatic caching and batching, which will not be available with django-nonrel.
If you decide not to use django-nonrel, the main differences are that Django models do not run under App Engine. You'll have to rewrite your models to inherit from App Engine's db.Model. Your forms that use Django's ModelForm will need to inherit from google.appengine.ext.db.djangoforms instead. Once you're on App Engine, you'd have to port back Django if you ever take your app somewher else.
If you already have a Django application you might want to check this out. You won't work with App Engine's datastore but Google Cloud SQL might fit your needs.

How to use multiple databases in a django application

I need to connect to multiple databases in my django application. I want to make connections to all the databases when ever I start the application and use those connections for all the requests. But I did not understand how to do that (setting these databases connections in global environment).
I tried to set database connections in settings.py and i tried to access those connections in my views using from django.conf import settings, But it is creating new database connections when ever new request comes.
Because of this my website always gives too many database connections error.
Can you please help me how to set these mysql connections in global environment?
Not only is this a planned feature, but one of this years GSOC projects is for multi database support. Alex Gaynor is working on it with Russell Keith-Magee as mentor. So I'm sure the result will be good.
The description of the project:
Django current has the low level hooks necessary for multiple database support,
but it doesn't have the high level API for using, nor any support
infrastructure, documentation, or tests. The purpose of this project would be
to implement the high level API necessary for the use of multiple databases in
Django, along with requisit documentation and tests.
The Django ORM does not support multiple databases.
There are some patches to modify Django with multiple database support called django-multidb You may have some luck with these, although they are a rather old.
Multiple DB support for Django is a planned feature.