Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @app.route("/api/posts")
- def posts_index():
- posts = Post.objects()
- return jsonify({
- "posts": json.loads(posts.to_json())
- })
- class User(Document):
- email = EmailField(required=True, unique=True)
- username = StringField(max_length=50, required=True, unique=True)
- password = StringField(required=True)
- created = DateTimeField(required=True, default=datetime.datetime.now())
- class Comment(EmbeddedDocument):
- content = StringField(max_length=5000)
- user = ReferenceField(User)
- created = DateTimeField(required=True, default=datetime.datetime.now())
- class Post(Document):
- title = StringField(max_length=120, required=True)
- user = ReferenceField(User, reverse_delete_rule=CASCADE)
- content = StringField(max_length=5000)
- comments = ListField(EmbeddedDocumentField(Comment))
- created = DateTimeField(required=True, default=datetime.datetime.now())
- {
- "posts": [
- {
- "_id": {
- "$oid": "5a5efd1f9ef3161fc64cb95a"
- },
- "comments": [],
- "content": "Lorem Ipsum",
- "created": {
- "$date": 1516178223018
- },
- "title": "Hello",
- "user": {
- "$oid": "5a5d0b9b9ef316220b6d9881"
- }
- },
- {
- "_id": {
- "$oid": "5a5efd1f9ef3161fc64cb95b"
- },
- "comments": [],
- "content": "Lorem Ipsum Dolor",
- "created": {
- "$date": 1516178223018
- },
- "title": "Hello Again",
- "user": {
- "$oid": "5a5d0b9b9ef316220b6d9881"
- }
- }
- ]
- }
Add Comment
Please, Sign In to add comment