Herramientas de usuario

Herramientas del sitio


fw:mongoengine

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Ambos lados, revisión anterior Revisión previa
Próxima revisión
Revisión previa
fw:mongoengine [2014/05/20 11:34]
alfred [Storing an object inside another]
fw:mongoengine [2020/05/09 09:25] (actual)
Línea 47: Línea 47:
   * ''​date_modified = DateTimeField(default=datetime.datetime.now)''​   * ''​date_modified = DateTimeField(default=datetime.datetime.now)''​
 More about this subject in: [[http://​docs.mongoengine.org/​guide/​defining-documents.html#​fields]] More about this subject in: [[http://​docs.mongoengine.org/​guide/​defining-documents.html#​fields]]
 +==== Storing an object ====
 +<code python>
 +Comment.objects.create(text='​Me gusta!'​)
 +</​code>​
 ==== Storing an object inside another ==== ==== Storing an object inside another ====
 <code python> <code python>
Línea 59: Línea 63:
     comments = ListField(EmbeddedDocumentField(Comment))     comments = ListField(EmbeddedDocumentField(Comment))
 </​code>​ </​code>​
-We also could use ''​GenericReferenceField''​ to referenci ​any kind of document:+We also could use ''​GenericReferenceField''​ to reference ​any kind of document:
 <code python> <code python>
 class Bookmark(Document):​ class Bookmark(Document):​
Línea 91: Línea 95:
 page.tags = ['​mongodb',​ '​mongoengine'​] page.tags = ['​mongodb',​ '​mongoengine'​]
 page.save() page.save()
 +</​code>​
 +==== Defining constraints ====
 +We can specify field uniqueness with ''​unique=True''​ and multi-fielduniqueness with ''​unique_with''​ (for a field name, or a list, or a tuple):
 +<code python>
 +class User(Document):​
 +    username = StringField(unique=True)
 +    first_name = StringField()
 +    last_name = StringField(unique_with='​first_name'​)
 </​code>​ </​code>​
 ===== Editing Data ===== ===== Editing Data =====
Línea 133: Línea 145:
 for post in Post.objects(tags='​mongodb'​):​ for post in Post.objects(tags='​mongodb'​):​
     print post.title     print post.title
 +</​code>​
 +
 +===== Others =====
 +
 +==== Django support ====
 +=== Connecting ===
 +You only need to call ''​connect()''​ somewhere in the settings module.
 +
 +If you were not using another Database backend you may need to add a dummy database:
 +<code python>
 +DATABASES = {
 +    '​default':​ {
 +        '​ENGINE':​ '​django.db.backends.dummy'​
 +    }
 +}
 +</​code>​
 +
 +=== Users ===
 +Django authentication classes are included as Document classes so they are compatible. To enable them you should add the following to your settings.py file:
 +<code python>
 +AUTHENTICATION_BACKENDS = (
 +    '​mongoengine.django.auth.MongoEngineBackend',​
 +)
 +</​code>​
 +Remember to call ''​set_password()''​ method in ''​User''​ class when assign a password value:
 +<code python>
 +instance.set_password(attrs.get('​password',​ instance.password))
 </​code>​ </​code>​
fw/mongoengine.1400585669.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)