验证NDB模型的重复属性中的重复项

时间:2016-05-03 08:41:00

标签: python-2.7 google-app-engine google-cloud-datastore app-engine-ndb

所以我有一个基于ndb.Model

的模型
from google.appengine.ext import ndb


class CFCSocialUser(ndb.Model):

    def clean_dob(self, value):
        if value.year < 1900:
            raise Exception("Year cannot be less than 1900")

    username = ndb.StringProperty(required=True)
    userid = ndb.IntegerProperty()
    email = ndb.StringProperty(required=True)
    date_of_birth = ndb.DateProperty(validator=clean_dob)

    @staticmethod
    def create_new_user(name, email, date_of_birth):
        app = CFCSocialUser(username=name,
                            email=email,
                            date_of_birth=date_of_birth,
                            id=name)
        return app.put()


    @staticmethod
    def create_new_user_id(name, email, date_of_birth, id):
        app = CFCSocialUser(username=name,
                            email=email,
                            date_of_birth=date_of_birth,
                            key=id)
        return app.put()

我有另一个基于ndb.Model的模型,看起来像

from google.appengine.ext import ndb
from mainsite.rainbow.models.CFCSocialUser import CFCSocialUser


class CFCSocialGroup(ndb.Model):
    name = ndb.StringProperty(required=True)
    created_on = ndb.StringProperty()
    created_by = CFCSocialUser()
    members = CFCSocialUser(repeated=True)

如何使用members功能向CFCSocialGroup的{​​{1}}属性添加验证器,例如CFCSocialUser中的出生日期属性验证器?

验证器将确保重复的CFCSocialUsers不属于同一组。

0 个答案:

没有答案