dtg78700 2018-07-14 05:09
浏览 56
已采纳

仅响应模型中的几个字段

Below is my Advertiser Model -

type Advertiser struct {
    ID            int       `json:"id" db:"id"`
    CreatedAt     time.Time `json:"created_at" db:"created_at"`
    UpdatedAt     time.Time `json:"updated_at" db:"updated_at"`
    Name          string    `json:"name" db:"name"`
    Email         string    `json:"email" db:"email"`
    ContactNumber string    `json:"contact_number" db:"contact_number"`
}

I have generated the Advertiser Resource and by default, it had brought-in the actions.

Now, in AdvertiserList action, I need all these fields to do some or the other calculation. But, finally, I would like to only respond with Name, Email and ContactNumber fields.

Remember, this is a List action, which means, we have an array of Advertiser.

Right now, my action does below-

func (v AdvertisersResource) List(c buffalo.Context) error {
    tx, ok := c.Value("tx").(*pop.Connection)
    if !ok {
        return errors.WithStack(errors.New("no transaction found"))
    }

    advertisers := &models.Advertisers{}

    q := tx.PaginateFromParams(c.Params())
    if err := q.All(advertisers); err != nil {
        return errors.WithStack(err)
    }

    c.Set("pagination", q.Paginator)

    return c.Render(200, r.JSON(advertisers))
}
  • 写回答

1条回答 默认 最新

  • dpylt7626401 2018-07-14 16:37
    关注

    I'm not sure if I understood your question correctly. However when marshaling to json if you have fields with tags json:"-" then that value is not returned.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行