求救Beego大神们,Beego orm 怎么限制Relation里返回的Variable?

type Accounts struct {
    Id            int           `orm:"column(account_id);pk"`
    UserName      string        `orm:"column(user_name);null" json:"omitempty"`
    Hash          string        `orm:"column(hash)" json:"omitempty"`
    IsLogin       bool          `orm:"column(is_login)" json:"omitempty"`
    ModifiedAt    time.Time     `orm:"column(modified_at);type(timestamp without time zone)" json:"omitempty"`
    CreatedAt     time.Time     `orm:"column(created_at);type(timestamp without time zone)" json:"omitempty"`
    Status        *DataStatus   `orm:"column(status);rel(fk)"`
    Role          *AccountRoles `orm:"column(role);rel(fk)"`
}

func GetAccountsById(id int) (v *Accounts, err error) {
    o := orm.NewOrm()
    v = &Accounts{Id: id}
    err = o.QueryTable("accounts").Filter("Id", id).RelatedSel().One(v, "account_id", "hash", "is_login", "status", "role")
    if err == nil {
        return v, nil
    }
    return nil, err
}

返回值为

{
    "Id": 1,
    "Hash": "$2a$13$LVfN1o9BTOCIqDBB/bVzg.3NrYRSHNuLaNp5LoJSg124yaq/xMznK",
    "IsLogin": false,
    "Status": {
        "Id": 1,
        "Description": "Account verified with email",
        "ModifiedAt": "2018-11-16T21:11:30.900571Z",
        "CreatedAt": "2018-11-16T21:11:30.900571Z"
    },
    "Role": {
        "Id": 1,
        "Name": "System Administrator",
        "ModifiedAt": "2018-11-16T21:11:29.408474Z",
        "CreatedAt": "2018-11-16T21:11:29.408474Z"
    }
}

理想的状态为

{
    "Id": 1,
    "Hash": "$2a$13$LVfN1o9BTOCIqDBB/bVzg.3NrYRSHNuLaNp5LoJSg124yaq/xMznK",
    "IsLogin": false,
    "Status": {
        "Id": 1,
        "Description": "Account verified with email"
    },
    "Role": {
        "Id": 1,
        "Name": "System Administrator"
    }
}

请问Beego ORM要怎么Filter Relation里的Variable? 类似 o.QueryTable("accounts").Filter("Id", id).RelatedSel().One(v, "account_id", "hash", "is_login", "status.data_status_id", "role.role_id")

已邀请:

jemygraw - Go爱好者

赞同来自:

这个一般可以在中间加一层过滤,就是Accounts那个对象不要ORM和JSON的共享。一般定义 AccountInput 表示输入, AccountOutput 表示输出,AccountPatch 表示更新,然后映射到ORM的那个对象定义为 Account。供参考。

要回复问题请先登录注册