Wednesday, October 21, 2015
Using django models in standalone application
One of my favorite things about Django is its loosely coupled design. It means that you can use each part independently and not affect others. And one of the parts that I often use in my applications is Djagno Models.
There is nothing difficult to use django models in standalone application. You just need to start new django project and then remove all unnecessary stuff leaving only models. Below I will show it on example.
1. Create new django project, let's call it 'standalone':
By default you will have the following files:
3. Open settings.py file and edit it by leaving only following settings:
4. Create models.py file (standalone/models.py):
5. Migrate to initialize database and create tables:
6. Now we can use django models in standalone application. Create test_app.py in root directory:
But there is one important note that you should keep in mind. If you're using DEBUG=True and not restarting your standalone application (for example doing heavy db writing in a loop), you will notice that your application leaking its memory. This is because Django keeps all queries of the current request and releases memory at the end of request. But since we have only 1 request (when we doing django.setup), nobody will release memory and Django will collect all queries bloating your standalone application memory until it gets killed by system. To avoid this I can recommend calling the following function after each db query (let me know if you know better solution):
There is nothing difficult to use django models in standalone application. You just need to start new django project and then remove all unnecessary stuff leaving only models. Below I will show it on example.
1. Create new django project, let's call it 'standalone':
django-admin startproject standalone
By default you will have the following files:
standalone/ manage.py standalone/ __init__.py settings.py urls.py wsgi.py2. Remove files urls.py and wsgi.py
3. Open settings.py file and edit it by leaving only following settings:
SECRET_KEY = ... #not really needed, just to avoid warnings DEBUG = ... INSTALLED_APPS = ("standalone",) MIDDLEWARE_CLASSES = [] DATABASES = ... #optional LANGUAGE_CODE = ... TIME_ZONE = ... USE_I18N = ... USE_L10N = ... USE_TZ = ...
4. Create models.py file (standalone/models.py):
from django.db import models class SampleModel(models.Model): def __unicode__(self): return self.pk
5. Migrate to initialize database and create tables:
python manage.py migrate
6. Now we can use django models in standalone application. Create test_app.py in root directory:
import os import django os.environ.setdefault("DJANGO_SETTINGS_MODULE", "models_standalone.settings") django.setup() from models_standalone.models import SampleModel if __name__=="__main__": print "SampleModel objects: " + str(len(SampleModel.objects.all()))
Summary
Finally your files tree will have the following structure:standalone/ manage.py test_app.py standalone/ __init__.py settings.pyAnd you can start writing your standalone application in test_app.py by doing any calls to django models.
But there is one important note that you should keep in mind. If you're using DEBUG=True and not restarting your standalone application (for example doing heavy db writing in a loop), you will notice that your application leaking its memory. This is because Django keeps all queries of the current request and releases memory at the end of request. But since we have only 1 request (when we doing django.setup), nobody will release memory and Django will collect all queries bloating your standalone application memory until it gets killed by system. To avoid this I can recommend calling the following function after each db query (let me know if you know better solution):
from django import db as django_db def reset_db_connection(): django_db.connection.close()If you're going to use only DEBUG=False, then nothing to worry about memory and everything should work perfectly fine.
What next?
To simplify creation process of standalone django models application, I made this simple package. In case you notice some problems with it or have suggestions to improve, just let me know or send pull request on github.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment