Use Django with MongoDB - python

I have a large mongo database with documents and i want to make Django website that is going to be a client to this mongo database but it can only filter (aggregate) and view information from database without any edit/update operations. I don't want to put other web site data (users' data, comments, other information) to mongo db.
I'm new to django framework and i wonder if it is better to connect mongodb and django using, for example, mongoengine and use two databases (one for the web site data, and the second one for external documents in mongodb) or use pymongo inside django to fetch data from external db and somehow transform it to djungo models?

Yup, you're spot on, in your case of needing two separate databases, it would be better to use mongoengine in order to use two separate databases. Check out this link. It goes over defining what database to use on a per-model basis so to say.
That most likely would work great. Basically you can query using the model the same way, regardless of the database is uses, but describe which database to use on the model itself.
Hope this helps!

Related

Changing Database in run time and making the changes reflect in Django in run time

I am developing a Cloud based data analysis tool, and I am using Django(1.10) for that.
I have to add columns to the existing tables, create new tables, change data-type of columns(part of data-cleaning activity) at the run time and can't figure out a way to update/reflect those changes, in run time, in the Django model, because those changes will be required in further analysis process.
I have looked into 'inspectdb' and 'syncdb', but all of these options would require taking the portal offline and then making those changes, which I don't want.
Please can you suggest a solution or a work-around of how to achieve this.
Also, is there a way in which I can select what database I want to work from the list of databases on my MySQL server, after running Django.
Django's ORM might not be the right tool for you if you need to change your schema (or db) online - the schema is defined in python modules and loaded once when Django's web server starts.
You can still use Django's templates, forms and other libraries and write your own custom DB access layer that manipulates a DB dynamically using python.

Django app as database web based UI

I'm planning to develop web UI something like iSQL*Plus-oracle but, for most common databases. Just take input query from user and return result with save options,etc.
So, for connecting to external data bases, what is advisable way,
1. Using django model and raw sql or,
2. with modules outside django - sqlalchemy+mysqldb,psycopg..?
Going through django documentation my understanding is db connections has to be in settings.py and I could not add from user input. Is my understanding is true or not?
I'm new to django not to python.
An ORM (something like Django's models or sqlalchemy) is a really helpful abstraction to help map tabular data in the database to the objects its being used to model in your code. It won't help with connecting to databases provided by the user since you won't know what the schema of the database is you're connecting to, nor what you are going to receive back from a query.
With django, the database defined in settings.py is used to store information related to your app such as user credentials, migrations as well as whatever else you define in your models.py files. So definitely don't try to change that dynamically as it is being used to store the state of your application for all users.
If you need to connect to external databases and run user-supplied queries, you can do that inside a view using the appropriate database driver. So psycopg2 for postgres would be fine.

Implementing a NoSQL backend

We have our own custom NoSQL storage system (sort of a document database), and we want a Django app above it.
I want to write a backend that will allow me to connect to our database. I don't care about the Django ORM because we have our own library for querying our database.
What would be the simplest & quickest way of doing this? Rewriting
& overriding the django.db.backends.base classes seems too much of
a hassle since we don't care about Django's ORM. What we basically
need is a way to keep a connection alive, and have the queries run
against the database from our views.py file
If we want to keep the Django auth system, should we rewrite that part so that Django creates the User model etc. in our DB? Perhaps we should have two databases, a simple RDBMS for the authentication part, and the rest of the data in our custom database?
Thanks in advance!

View data not created by the model class appengine python

I have data in my database added by non model class method (Say using Sqlite3) running locally
Is there any way I can view this data or add it to a model schema?
So same database, table created and populated externally, any way to access this data?
there's a variety of sqlite tools that you can use to directly access the database.
https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/
note that this will only work on the dev server, where sqlite is used to emulate the datastore.
The better option is to look at the low level datastore API. I don't know if there's online documentation, but you can look through the python code in the App Engine SDK, look for google.appengine.api.datastore, that has all the basic datastore commands.

Mongo DB or Couch DB with django for building an app that is similar to top coder?

This is what I have :-
Ubuntu 11.10.
Django 1.3
Python 2.7
What I want to do is build an app that is similar to top-coder and I have the skeletal version of the app sketched out. The basic requirements would be:-
1. Saving the code.
2. Saving the user name and ranks.(User-profile)
3. Should allow a teacher to create multiple choice questions too.( Similar to Google docs).
I have basic knowledge of Django and have built couple of (basic) apps before. Rather than building an online tool, is it possible to build something very similar to conf2py that sits on top of web2py, in Django.
Lets call this small project examPy( I know, very original), is it possible to build an app that acts more a plug-in to Django or is my concept of Django absolutely wrong?
The primary question being:
As I want to learn a new DB and have worked on postgres in Django, should I chose CouchDB or MongoDB for Django?
Answers can be explanations or links to certain documentations or blogs that can tell me the pros and cons.
General Differences
Comparing MongoDB and CouchDB
MongoDB VS CouchDB
Battle of the NoSQL Stars
Django Specific
MongoDB for Django-nonrel
Django-MongoDB Engine
MongoDB Hearts Django?
An Introduction to Using CouchDB with Django
Using CouchDB with Django
All my research points me towards the idea that Mongo and Couch are similar enough that your choice would probably boil down to personal (subjective) preference even over the use-case. Personally, I've developed a CouchDB fetish and am looking for a reason to use it.
The key factor influencing your decision should probably be which noSQL solution has the most mature ORM framework for Django?
I've used mongo-engine with Django but you need to create a file specifically for Mongo documents eg. Mongo_models.py. In that file you define your Mongo documents. You then create forms to match each Mongo document. Each form has a save method which inserts or updates whats stored in Mongo. Django forms are designed to plug into any data back end ( with a bit of craft ).
If you go this route you can dodge Django non-rel which is still not part of Django 1.4. In addition I believe django-nonrel is on hiatus right now.
I've used both CouchDB and Mongo extensively. CouchDB has a lovely interface. My colleague is working on something similar for Mongo. Mongo's map and reduce are far faster than CouchDB. Mongo is more responsive loading and retrieving data. The python libraries for Mongo are easier to get working with ( both pymongo and mongo-engine are excellent )
Be sure you read the Mongo production recommendations! Do not run one instance on the same node as Django or prepare to be savagely burned when traffic peaks. Mondo works great with Memcache/Redis where one can store reduced data for rapid lookups.
BEWARE: If you have very well defined and structured data that can be described in documents or models then don't use Mongo. Its not designed for that and something like PostGreSQL will work much better.
I use PostGreSQL for relational or well structured data because its good for that. Small memory footprint and good response.
I use Redis to cache or operate in memory queues/lists because its very good for that. great performance providing you have the memory to cope with it.
I use Mongo to store large JSON documents and to perform Map and reduce on them ( if needed ) because its very good for that. Be sure to use indexing on certain columns if you can to speed up lookups.
Don't use a circle to fill a square hole. It won't fill it.
I've used CouchDB with Django for a production application. Couch is fine and has some great ideas, but I'm moving that app over to MongoDB. Why? There is support for Mongo in the Django community. Django-nonrel has a MongoDB backend. Using Django-toolbox I can embed models in models and have some basic admin support.
If I remember correctly, Django-nonrel will eventually be rolled into Django core. In five years time I see much more support for Mongo in Django than Couch. Of course that can change, but I see Mongo a better fit.

Categories

Resources