在 Phoenix 中使用 Ecto 获取数据时仅获取特定字段
Get only specific fields when fetching data using Ecto in Phoenix
我正在尝试 return 一些 JSON 数据到我在凤凰城的一个 API 电话中。我正在获取 Subject
的所有记录并发送它们,但是 Ecto
return 有一些我不想要的额外字段。
我能做什么:
- 仅获取特定属性(例如,仅
id
和 name
)
- 我的回复中没有出现不必要的字段(例如
__meta__
和 __owner__
)
这是我的 Controller
:
# Controller
def index(conn, _) do
subjects = Subject |> Repo.all
conn |> render subjects: subjects
end
这是我的 View
:
# View
def render("index.json", %{subjects: subjects}) do
subjects
end
这是我的回复:
[
{
"teachers": {
"__owner__": "Elixir.MyApp.Subject",
"__field__": "teachers",
"__cardinality__": "many"
},
"updated_at": "2015-06-20T15:32:20Z",
"topics": {
"__owner__": "Elixir.MyApp.Subject",
"__field__": "topics",
"__cardinality__": "many"
},
"name": "Physics",
"inserted_at": "2015-06-20T15:32:20Z",
"id": 1,
"__meta__": {
"state": "loaded",
"source": "subjects"
}
},
{
"teachers": {
"__owner__": "Elixir.MyApp.Subject",
"__field__": "teachers",
"__cardinality__": "many"
},
"updated_at": "2015-06-20T15:37:59Z",
"topics": {
"__owner__": "Elixir.MyApp.Subject",
"__field__": "topics",
"__cardinality__": "many"
},
"name": "Chemistry",
"inserted_at": "2015-06-20T15:37:59Z",
"id": 2,
"__meta__": {
"state": "loaded",
"source": "subjects"
}
},
{
"teachers": {
"__owner__": "Elixir.MyApp.Subject",
"__field__": "teachers",
"__cardinality__": "many"
},
"updated_at": "2015-06-20T15:38:41Z",
"topics": {
"__owner__": "Elixir.MyApp.Subject",
"__field__": "topics",
"__cardinality__": "many"
},
"name": "Mathematics",
"inserted_at": "2015-06-20T15:38:41Z",
"id": 3,
"__meta__": {
"state": "loaded",
"source": "subjects"
}
},
{
"teachers": {
"__owner__": "Elixir.MyApp.Subject",
"__field__": "teachers",
"__cardinality__": "many"
},
"updated_at": "2015-06-22T15:40:17Z",
"topics": {
"__owner__": "Elixir.MyApp.Subject",
"__field__": "topics",
"__cardinality__": "many"
},
"name": "Biology",
"inserted_at": "2015-06-22T15:40:17Z",
"id": 4,
"__meta__": {
"state": "loaded",
"source": "subjects"
}
}
]
将您的观点更改为:
def render("index.json", %{subjects: subjects}) do
Enum.map(subjects, &Map.take(&1, [:id, :name]))
end
此外,您还可以要求 Ecto return 将控制器更改为以下字段的子集:
def index(conn, _) do
subjects = from(s in Subject, select: %{id: s.id, name: s.name}) |> Repo.all
conn |> render subjects: subjects
end
我正在尝试 return 一些 JSON 数据到我在凤凰城的一个 API 电话中。我正在获取 Subject
的所有记录并发送它们,但是 Ecto
return 有一些我不想要的额外字段。
我能做什么:
- 仅获取特定属性(例如,仅
id
和name
) - 我的回复中没有出现不必要的字段(例如
__meta__
和__owner__
)
这是我的 Controller
:
# Controller
def index(conn, _) do
subjects = Subject |> Repo.all
conn |> render subjects: subjects
end
这是我的 View
:
# View
def render("index.json", %{subjects: subjects}) do
subjects
end
这是我的回复:
[
{
"teachers": {
"__owner__": "Elixir.MyApp.Subject",
"__field__": "teachers",
"__cardinality__": "many"
},
"updated_at": "2015-06-20T15:32:20Z",
"topics": {
"__owner__": "Elixir.MyApp.Subject",
"__field__": "topics",
"__cardinality__": "many"
},
"name": "Physics",
"inserted_at": "2015-06-20T15:32:20Z",
"id": 1,
"__meta__": {
"state": "loaded",
"source": "subjects"
}
},
{
"teachers": {
"__owner__": "Elixir.MyApp.Subject",
"__field__": "teachers",
"__cardinality__": "many"
},
"updated_at": "2015-06-20T15:37:59Z",
"topics": {
"__owner__": "Elixir.MyApp.Subject",
"__field__": "topics",
"__cardinality__": "many"
},
"name": "Chemistry",
"inserted_at": "2015-06-20T15:37:59Z",
"id": 2,
"__meta__": {
"state": "loaded",
"source": "subjects"
}
},
{
"teachers": {
"__owner__": "Elixir.MyApp.Subject",
"__field__": "teachers",
"__cardinality__": "many"
},
"updated_at": "2015-06-20T15:38:41Z",
"topics": {
"__owner__": "Elixir.MyApp.Subject",
"__field__": "topics",
"__cardinality__": "many"
},
"name": "Mathematics",
"inserted_at": "2015-06-20T15:38:41Z",
"id": 3,
"__meta__": {
"state": "loaded",
"source": "subjects"
}
},
{
"teachers": {
"__owner__": "Elixir.MyApp.Subject",
"__field__": "teachers",
"__cardinality__": "many"
},
"updated_at": "2015-06-22T15:40:17Z",
"topics": {
"__owner__": "Elixir.MyApp.Subject",
"__field__": "topics",
"__cardinality__": "many"
},
"name": "Biology",
"inserted_at": "2015-06-22T15:40:17Z",
"id": 4,
"__meta__": {
"state": "loaded",
"source": "subjects"
}
}
]
将您的观点更改为:
def render("index.json", %{subjects: subjects}) do
Enum.map(subjects, &Map.take(&1, [:id, :name]))
end
此外,您还可以要求 Ecto return 将控制器更改为以下字段的子集:
def index(conn, _) do
subjects = from(s in Subject, select: %{id: s.id, name: s.name}) |> Repo.all
conn |> render subjects: subjects
end