Mongoengine + Flash Development tips (1)

Time:2020-8-24

1. The data model class refers to itself, such as a list of people who are concerned

carelist = ListField(ReferenceField('self'))

2. Query the reference field. The reference field can only query the objectid, and other fields cannot be queried, unless the objectid of the target object is found first, and then the reference field is used to query in the following way

posts = Post.objects(author=g.user.id)

3. The first function must be used to query a single object, otherwise the result is a dataset. Even if there is only one, some operations supported by single data are not supported

User.objects(id=userid).first()

4. Delete the referenced data associated with a domain

ReferenceField(User,reverse_delete_rule=CASCADE)

5. Query the subset of data set, that isselect a,b from foo

post = BlogPost.objects(...).only("title", "author.name")