appengine:将ndb模型转换为go-lang-stru

2024-04-25 10:30:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我在appengine上有一个python模块和一个go模块。go模块相当简单,只提供了一个只读的搜索接口到python模块填充的数据存储。在

如何将以下ndb模型转换为go结构:

class Course(ndb.Model):
    name = ndb.StringProperty()
    neat_name = ndb.StringProperty(required=True)
    country = ndb.KeyProperty(kind=Country, required=True)
    university = ndb.KeyProperty(kind=University, required=True)
    faculty = ndb.KeyProperty(kind=Faculty, required=True)
    department = ndb.KeyProperty(kind=Department, required=True)
    stage = ndb.KeyProperty(kind=Stage, required=True)

    legacy_id = ndb.StringProperty()
    course_title = ndb.StringProperty(required=True, indexed=False)
    course_description = ndb.TextProperty(required=True)
    course_link = ndb.StringProperty(required=True, indexed=False)

    #0-5 or None or not has attribute.
    course_rating_ = ndb.FloatProperty()
    course_review_count_ = ndb.IntegerProperty()

首先,我要:

^{pr2}$

对于ndb.KeyProperty属性,我是否只在struct中使用string?&我要解析这个字符串-这是直接的吗?在

我也可以忽略required=True&;indexed=False选项吗?显然因为我不写任何东西?在


Tags: 模块or数据namefalsetruegorequired
2条回答

每^{{{a1},String(一个短串最多500个字符,默认索引索引索引)地图去去^{{};^{{}(一个长字符串达1MB,不索引)也去{{}},但始终与{{};对于数据存储{{}}有{{{}},见{{a2};对于{{a2};对于{{a2};对于{{a2};对于^{{cd8>}{{{}},int64};对于{};对于}{{{{{{},float64(您可以使用较短的int和float,但是数据存储对每个都使用64位,所以您可以好吧:-). 在

Also can I just ignore the required=True & indexed=False options?

required是的,但我相信,使用https://cloud.google.com/appengine/docs/go/datastore/reference,您确实必须对Text使用选项noindex,因为必须指示长度超过512(unicode)字符的字符串。在

不确定是哪个版本的go及其datastore包强制执行此约束,但即使当前的版本没有这样做,还是更安全地遵守它,否则你的应用程序可能会因为一个简单的Go版本升级而崩溃!-)在

以下是代码-它也在生产和本地工作:

type Course struct {
    Name              string         `datastore:"name"`
    NeatName          string         `datastore:"neat_name"`
    Country           *datastore.Key `datastore:"country"`
    University        *datastore.Key `datastore:"university"`
    Faculty           *datastore.Key `datastore:"faculty"`
    Department        *datastore.Key `datastore:"department"`
    Stage             *datastore.Key `datastore:"stage"`
    LegacyId          string         `datastore:"legacy_id"`
    CourseTitle       string         `datastore:"course_title,noindex"`
    CourseDescription string         `datastore:"course_description"`
    CourseLink        string         `datastore:"course_link,noindex"`
    CourseRating      float64        `datastore:"course_rating_"`
    CourseReviewCount int64          `datastore:"course_review_count_"`
}

以及

^{pr2}$

相关问题 更多 >