Django-like abstract database API for non-Django projects - python

I love the abstract database API that comes with Django, I was wondering if I could use this (or something similar) to model, access, and manage my (postgres) database for my non-Django Python projects.

What you're looking for is an object-relational mapper (ORM). Django has its own, built-in.
To use Django's ORM by itself:
Using the Django ORM as a standalone component
Use Django ORM as standalone
Using settings without setting DJANGO_SETTINGS_MODULE
If you want to use something else:
What are some good Python ORM solutions?

Popular stand-alone ORMs for Python:
SQLAlchemy
SQLObject
Storm
They all support MySQL and PostgreSQL (among others).

I especially like SQLAlchemy with following tools:
Elixir (declarative syntax)
Migrate (schema migration)
They really remind me of ActiveRecord.

Related

Djongo migrate models cons

Its's nice to see mongodb is connected with django but doesn't support completely. I explain why so because when you install any third party packages to your project it could written for Django models not Djongo which will fall you in Database error while migrating you apps.
djongo.database.DatabaseError
Do we have any solution for this case?
Well, in the general case, the solutions you have are, in increasing order of difficulty:
Get rid of Djongo. Use Django with an SQL database, as that's what it's designed for. You can still use MongoDB for whatever data you might have in there, but the main Django database is better off being SQL.
Don't use packages that don't work with Djongo.
Fix the packages that don't work with Djongo to work with it.
I would recommend going for (1)...

How to handle database migration scripts in Tornado framework.?

I have been searching for libraries that can handle database migration for tornado framework. How is the database migration handled in Tornado framework ?
I was working on Yii Framework before and it has a very convenient CLI tool to handle Database migration.
I am looking for something like that.
I highly recommend Alembic. It was originally written to support SQLAlchemy, but you don't need to use SQLAlchemy as long you have a relational database it supports. That said, you will need to understand SQLAlchemy Core to work with Alembic, but that is generally a useful skill to have.

How can I use djcelery.schedulers without Django project?

I want to use dynamic scheduler management for celery. I know djcelery have that functionality with database support.
But currently I do not use Django, but Flask. I couldn't find out Flask project or implementation with djcelery.schedulers.
Is it possible to use djcelery and implement dynamic scheduler management system without Django?
Short answer: No, but...
You have to use django. The scheduler's entries are instances of django models so you would have to setup djcelery app somehow (see this code: https://github.com/celery/django-celery/blob/master/djcelery/schedulers.py) Also you won't have the admin interface to add scheduler's entries.
This is just a guess, but you can try setting django's ORM standalone and syncing djcelery's models. (see: Use Django ORM as standalone)
You can also implement your own scheduler following the structure of djcelery/schedulers.py
Also see: Can celery celerybeat use a Database Scheduler without Django?
You can check out this flask-djcelery. It configures djcelery with flask, allows using django admin and also provide a browseable rest api for managing tasks.

Django with MongoDB

OK.. i am starting a project in django 1.4 and i want MongoDB as my backend. after a half a day of google search, i figured out that mongoengine is a best option(as it is an active project and provides a django like orm)
Now the problem is
1. I cant find any good step-by-step setup guide to integrate mongoengine with a django project.
I understand, using mongoengine means that i am replacing django orm and there is no need to do syncdb. now this project have a multi-tenant architecture (*.domain.com) which i am gonna resolve using a middleware..also a considerable part of this project will work on django admin.
Question: will replacing django orm with mongoengine going to affect django admin and other operations(such as middleware, authentication etc.) ?
I am open to suggestions and criticism as well.
Django Admin is designed to work with the Django ORM only. Using MongoEngine and no Django ORM will mean you don't get the automatic admin interface. Other middleware might use the Django ORM or be sufficiently abstracted enough to allow you to plugin MongoEngine - eg: Sessions and Authentication.
There are some helpers for Django in MongoEngine - but its by no means complete or designed to be a drop in replacement for the Django ORM.
For more information see this presentation from Django Conf Finland: http://staltz.github.io/djangoconfi-mongoengine
Just in case, situation has changed and there is a solution now for this problem, namely django-mongoadmin.
A Guide to integrating Django with MongoDB
The way to connect Django with MongoDB by adding just one line of code:
First install djongo:
pip install djongo
Then run your migrations:
manage.py make migrations
manage.py migrate
and finally add to your settings file:
DATABASES = {
‘default’: {
‘ENGINE’: ‘djongo’,
‘NAME’: ‘your-db-name’,
}
}
It is as simple as that!
If you want to manipulate MongoDB using Django Admin, simply fire it up:
manage.py runserver
Goto: http://localhost:8000/admin/
Manipulate your embedded models as shown in this screenshot:
For more information do checkout the djongo documentation.
You should definitely consider the pros and cons of using a NEW framework (like MongoEngine) vs using inbuilt Django ORM. Do read at this tutorial before considering to adopt MongoEngine as suggested by other knowledgeable members! No offence!
Let me know if you agree with this approach in the comments :)

Is there a CherryPy equivalent to Django Model.objects.filter()?

I know that in Django I can fetch objects from the DB with something like ModelName.objects.filter().
Is there an analogous pattern in CherryPy?
Yes but not native. There are a couple of python ORM's that appear to work great with cherry pie with similar syntax to django. SQLAlchemy is an extremely popular very well supported ORM. It has a huge active community and is probably the de-facto python ORM. THere is a tool posted on cherrypy site that helps with integration.
From wikipedia:
Object-relational mappers:
SQLAlchemy — a database backend and ORM for Python applications. TurboGears 2.x uses CherryPy as server and SQLAlchemy as its default ORM.[13]
SQLObject — a popular ORM for providing an object interface to your database. Supports a number of common database backends: included in the distribution are MySQL, PostgreSQL, SQLite, Sybase SQL Server, MaxDB, Microsoft SQL Server and Firebird. TurboGears 1.x uses CherryPy as server and SQLObject as ORM.[14]
Storm — the ORM from Canonical Ltd. (makers of Ubuntu)
Dejavu[15] — a public domain, thread-safe ORM for Python applications

Categories

Resources