Django with MongoDB - python

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 :)

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)...

Advice on how to handle PostgreSQL database in Django app?

For some background, I worked on a Django app that had a SQLite3 database. I just created models in Django using the python manage.py startapp model_name, and just directly manipulated the database by creating API endpoints in python. I would then make API requests and change my database like that.
But this time, I've created a Django app that has a PostgreSQL database. I managed to connect my database to Django after figuring out how to install psycopg2. Then, I created a Users table in PostgreSQL through the command line. I want to get this table to show on my Django admin site, and hopefully repeat the same method for the other app. But the Django admin site can render only Django models that have been registered to the admin. So now, I'm wondering if there is a better method for working with PostgreSQL in Django.
I saw SO posts suggesting inspectdb, but this creates a large models.py file generating other classes for my app. I also couldn't successfully register the Users model to the Django admin site. I am now curious if I am working in the opposite direction. Should I be generating User models in Django and then from there, generate my PostgreSQL database?
Basically, can someone explain how to work with a PostgreSQL database in a Django app?
Can you be more specific with your question?
If you are confused on how to register the User table in django admin, you can register it in admin.py. But the method of registering depends on the type of User model you are using. You can see the docs for Custom User model.
And to get the user table in your database, you need to do migrate:
python manage.py makemigrations
python manage.py migrate
Let me know if you need any help.

Django Rest Framework standalone?

Do I need to have a django website in order to use django rest framework, or can I use DRF by itself as a standalone app? Sorry but it is not so obvious to me. thanks for the help.
There are some parts you can use without Django though it might need to be installed.
This question feels like this isn't the real question. Why would you need DRF without Django ?
Well It's not that common but you can use Django framework's modules and classes in standalone scripts by importing Django.
For that you will need to add at the beginning of your code:
import django
django.setup()
django rest framework is a wrapper of django from rest APIs. django is required for django rest framework

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-like abstract database API for non-Django projects

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.

Categories

Resources