0

I want to understand why do we need to define a data model like the following. What is the purpose and advantages of having this?

class Gender(Document):
    name = StringField(max_length=60, required=True, unique=True)

    def __unicode__(self):
        return self.name

    def __repr__(self):
        return self.name

    def __str__(self):
        return self.name

I am building a flask api using MongoDb as the database.

4

1 回答 1

0

MongoDB 没有什么要求您这样做,但您似乎正在使用 MongoEngine,它基本上是围绕Document类构建的。

它只是 Python 代码中 MongoDB 模式的一种表示,因此您可以使用诸如.save()执行数据验证等方法。

基本上,它是一种以面向对象的方式与 MongoDB 数据库交互的方式。你不需要这样做,但它非常方便。

于 2017-03-25T18:50:44.540 回答