1

我在 Flask 中定义了两个 Mongo 文档:

class Category(db.Document):
    name = db.StringField(max_length=255, required=True)
    created = db.DateTimeField(default=datetime.datetime.now, required=False)

    def __unicode__(self):
        return self.name

class Event(db.Document):
    description = db.StringField(max_length=255, required=True)
    created = db.DateTimeField(default=datetime.datetime.now, required=False)
    categories = db.ListField(db.ReferenceField(Category),required=False)
    meta = {
        'indexes': ['-description','created'],
        'ordering': ['-created']
    }

我使用一个很小的 ​​REST api 来为我的 Backbone 模型提供服务。

window.EventModel = Backbone.Model.extend({
    defaults: {},
    initialize: function Event(){},
    validate: function(a){},
    url: function(){
        return this.id ? '/events/' + this.id : '/events';
    }

})

window.CategoryModel = Backbone.Model.extend({
    defaults: {},
    initialize: function Expense(){},
    validate: function(a){},
    url: function(){
        [...]
    }

})

这是我的 JSON 响应:

[{
        "description": "MyEvent",
        "created": {"$date": 1338404831615},
        "id": "4fc653ffbe51030887000004",
        "categories": [{
                "$ref": "category", 
                "$id": {"$oid": "4fc652efbe51030887000001"}
        }, {
                "$ref": "category",
                "$id": {"$oid": "4fc65304be51030887000002"}
        }]
}]

我想知道如何将 Event 模型与 Category 模型链接到 Backbone?

注意:在这种情况下,骨干关系无法按预期工作。

有任何想法吗?非常感谢!

4

0 回答 0