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 10:40]
alfred [Editing Data]
fw:mongoengine [2020/05/09 09:25] (actual)
Línea 45: Línea 45:
 === Other field objects === === Other field objects ===
   * ''​tags = ListField(StringField(max_length=30))'',​ a ''​ListField''​ is a field object that allows to have a list of other field objects (including other lists).   * ''​tags = ListField(StringField(max_length=30))'',​ a ''​ListField''​ is a field object that allows to have a list of other field objects (including other lists).
 +  * ''​date_modified = DateTimeField(default=datetime.datetime.now)''​ 
 +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 58: Línea 63:
     comments = ListField(EmbeddedDocumentField(Comment))     comments = ListField(EmbeddedDocumentField(Comment))
 </​code>​ </​code>​
 +We also could use ''​GenericReferenceField''​ to reference any kind of document: 
 +<code python>​ 
 +class Bookmark(Document):​ 
 +    bookmark_object = GenericReferenceField() 
 +</​code>​
 ==== Cascade delete ==== ==== Cascade delete ====
 When we define a ''​ReferenceField''​ we can indicate ''​reverse_delete_rule=CASCADE''​ to delete all the objects when a referenced object is deleted (it does not work with MapFields neither DictFields). When we define a ''​ReferenceField''​ we can indicate ''​reverse_delete_rule=CASCADE''​ to delete all the objects when a referenced object is deleted (it does not work with MapFields neither DictFields).
Línea 68: Línea 77:
     comments = ListField(EmbeddedDocumentField(Comment))     comments = ListField(EmbeddedDocumentField(Comment))
 </​code>​ </​code>​
 +Other constans we can pass:
 +  * ''​mongoengine.DO_NOTHING''​
 +  * ''​mongoengine.DENY''​
 +  * ''​mongoengine.NULLIFY''​
 +  * ''​mongoengine.CASCADE''​
 +  * ''​mongoengine.PULL''​
 +==== Dynamic documents ====
 +DynamicDocument documents work like Document but any data or attributes set to them will also be saved (fields cannot start with _ ).
 +<​code>​
 +from mongoengine import *
  
 +class Page(DynamicDocument):​
 +    title = StringField(max_length=200,​ required=True)
 +
 +# Create a new page and add tags
 +page = Page(title='​Using MongoEngine'​)
 +page.tags = ['​mongodb',​ '​mongoengine'​]
 +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>​
 ===== Editing Data ===== ===== Editing Data =====
 ==== Adding documents ==== ==== Adding documents ====
Línea 110: 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.1400582400.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)