pip install hg+https://bitbucket.org/wkornewald/django-nonrel
pip install hg+https://bitbucket.org/wkornewald/djangotoolbox
pip install git+https://github.com/django-nonrel/mongodb-engine
DATABASES = { 'default' : { 'ENGINE' : 'django_mongodb_engine', 'NAME' : 'my_database' } }
from djangotoolbox.fields import ListField class Post(models.Model): ... tags = ListField()
>>> Post(tags=['django', 'mongodb'], ...).save() >>> Post.objecs.get(...).tags ['django', 'mongodb']
class Post(models.Model): ... edited_on = ListField(models.DateTimeField())
>>> post = Post(edited_on=['1010-10-10 10:10:10']) >>> post.save() >>> Post.objects.get(...).edited_on [datetime.datetime([1010, 10, 10, 10, 10, 10])]
from djangotoolbox.fields import EmbeddedModelField, ListField class Post(models.Model): ... comments = ListField(EmbeddedModelField('Comment')) class Comment(models.Model): ... text = models.TextField()
from djangotoolbox.fields import DictField class Image(models.Model): ... exif = DictField()
>>> Image(exif=get_exif_data(...), ...).save() >>> Image.objects.get(...).exif {u'camera_model' : 'Spamcams 4242', 'exposure_time' : 0.3, ...}
class Poll(models.Model): ... votes = DictField(models.IntegerField())
>>> Poll(votes={'bob' : 3.14, 'alice' : '42'}, ...).save() >>> Poll.objects.get(...).votes {u'bob' : 3, u'alice' : 42}
Post.objects.filter(...).update(title='Everything is the same')
.update(..., {'$set': {'title': 'Everything is the same'}})
Post.objects.filter(...).update(visits=F('visits')+1)
.update(..., {'$inc': {'visits': 1}})
from djangotoolbox.fields import EmbeddedModelField from django_mongodb_engine.contrib import MongoDBManager class Point(models.Model): latitude = models.FloatField() longtitude = models.FloatField() class Place(models.Model): ... location = EmbeddedModelField(Point) objects = MongoDBManager()
>>> here = {'latitude' : 42, 'longtitude' : 3.14} >>> Place.objects.raw_query({'location' : {'$near' : here}})
from django_mongodb_engine.contrib import MongoDBManager class FancyNumbers(models.Model): foo = models.IntegerField() objects = MongoDBManager()
FancyNumbers.objects.raw_update({}, {'$bit' : {'foo' : {'or' : 42}}})
Source: https://habr.com/ru/post/160191/
All Articles