Sometimes you need to test some model features without a complete django app installation. Just play only with the model object. With these small script you have a complete in memory django installation.
Some Links: http://www.djangosnippets.org/snippets/1044/ (en) http://www.jensdiemer.de/permalink/150/mein-blog/99/django-db-model-test/ (de) http://www.python-forum.de/viewtopic.php?f=3&t=15649 (de)
See also: https://github.com/readevalprint/mini-django/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | #!/usr/bin/env python
# coding: utf-8
"""
Standalone django model test with a 'memory-only-django-installation'.
You can play with a django model without a complete django app installation.
http://www.djangosnippets.org/snippets/1044/ (en)
http://www.jensdiemer.de/permalink/150/mein-blog/355/mini-django-db-model-test-app/ (de)
http://www.python-forum.de/viewtopic.php?f=3&t=15649 (de)
see also:
https://github.com/readevalprint/mini-django/
Output from this current example code:
-------------------------------------------------------------------------------
*** call 'syncdb':
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_testmodel
Creating table django_content_type
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
-------------------------------------------------------------------------------
new instance: TestModel entry: 'test'
instance data:
id: None
date_created: None
last_update: None
user: <User: test>
content: 'test'
pk: 1
create date: 2012-05-10T05:14:06.820967
-------------------------------------------------------------------------------
- END -
-------------------------------------------------------------------------------
"""
import os
BASE_PATH = os.path.abspath(os.path.dirname(__file__))
APP_LABEL = os.path.splitext(os.path.basename(__file__))[0]
os.environ["DJANGO_SETTINGS_MODULE"] = APP_LABEL
# settings:
DEBUG = TEMPLATE_DEBUG = True
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
APP_LABEL,
)
DATABASES = {
'default': {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:",
}
}
if __name__ == "__main__":
#______________________________________________________________________________
# TestModel model classes:
from django.db import models
from django.contrib.auth.models import User
class TestModel(models.Model):
date_created = models.DateTimeField(auto_now_add=True)
last_update = models.DateTimeField(auto_now=True)
user = models.ForeignKey(User, blank=True, null=True)
content = models.TextField()
def __unicode__(self):
return u"TestModel entry: '%s'" % self.content
class Meta:
app_label = "auth" # Hack: Cannot use an app_label that is under South control, due to http://south.aeracode.org/ticket/520
#------------------------------------------------------------------------------
print " *** call 'syncdb':"
from django.core import management
management.call_command('syncdb', verbosity=1, interactive=False)
print "-"*79
# print "_"*79
# print " *** diffsettings:"
# management.call_command('diffsettings', verbosity=1, interactive=False)
# print
# print "-"*79
#__________________________________________________________________________
# play with TestModel:
user = User(username="test")
user.save()
instance = TestModel(content="test", user=user)
print "new instance:", instance
print "instance data:"
for field in instance._meta.fields:
print "%17s: %s" % (field.name, repr(getattr(instance, field.name)))
instance.save()
print "pk:", instance.pk
print "create date:", instance.date_created.isoformat()
print "-"*79
print "- END -"
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 8 months ago
Comments
Please login first before commenting.