This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mongoengine.document import Document, EmbeddedDocument | |
class DecisionDocument(EmbeddedDocument): | |
guilt = fields.StringField(required=True) | |
details = fields.DictField() | |
meta = {'allow_inheritance': False} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"_id" : { | |
"set" : ISODate("2012-02-08T18:37:52.191Z"), | |
"db_key" : "_id" | |
}, | |
"judge" : { | |
"set" : ISODate("2012-02-08T18:37:52.191Z"), | |
"db_key" : "j" | |
}, | |
"decision" : { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"_id" : { | |
"set" : ISODate("2012-02-08T18:37:52.191Z"), | |
"db_key" : "_id" | |
}, | |
"decision" : { | |
"set" : ISODate("2012-02-08T18:37:52.191Z"), | |
"db_key" : "d", | |
"embedded_fields" : { | |
"guilt" : { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from test.app import DecisionDocument, TrialDocument | |
subdoc = DecisionDocument(guilt='definitely', details={'bad_person_score': 'Very Bad Man'}) | |
doc = TrialDocument(decision=[subdoc], reporter='Tom', jury='peers') | |
doc.save() | |
# -----Object Created------ | |
{ | |
"_id" : ObjectId("4f32c2f4d0ba3a4b5e000000"), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mongoengine.base import TopLevelDocumentMetaclass | |
class CompressedKeyDocumentMetaclass(TopLevelDocumentMetaclass): | |
def __new__(cls, name, bases, attrs): | |
""" | |
MongoEngine Document Classes access the 'TopLevelDocumentMetaclass' | |
__metaclass__. We allow that metaclass to set attrs on the class | |
and then compress the fields in the event that the instantiated | |
Document Class contains the meta attr 'compress_keys' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"_id" : ObjectId("4f15adc6e9f3d887f2ba7a31"), | |
"uid" : 274790, | |
"did" : 720717, | |
"city_id" : 4, | |
"score" : 10000, | |
"published" : ISODate("2012-01-17T17:20:54Z"), | |
"active" : true, | |
"recs" : [ | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
class Author(models.Model): | |
first_name = models.CharField(max_length=255) | |
last_name = models.CharField(max_length=255) | |
books = models.ManyToManyField('Book') | |
class Book(models.Model): | |
title = models.CharField(max_length=255) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
book_store = BookStore.objects.get(name="B&N") | |
authors = Author.objects.filter(book__in=book_store.books.all()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db.models.fields.related import ReverseManyRelatedObjectsDescriptor | |
from django.db.models import ManyToManyField | |
class NewReverseManyRelatedObjectsDescriptor(ReverseManyRelatedObjectsDescriptor): | |
def make_id_list(self, obj): | |
through = self.field.rel.through | |
name = self.field.m2m_field_name() | |
related_name =self.field.m2m_reverse_field_name() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT `author`.`id`, `author`.`first_name`, `author`.`last_name` | |
FROM `author` | |
INNER JOIN `author_books` | |
ON (`author`.`id` = `author_books`.`author_id`) | |
WHERE `author_books`.`book_id` | |
IN (SELECT U0.`book_id` FROM `bookstore_books` U0 WHERE U0.`bookstore_id` = 1 ) |
OlderNewer