在Phoenix中使用Ecto获取数据时仅获取特定字段

She*_*yar 6 elixir ecto phoenix-framework

我正试图在Phoenix的一个API调用中返回一些JSON数据.我正在获取所有记录Subject并发送它们但Ecto返回一些我不想要的额外字段.

我能做些什么:

  • 仅获取特定属性(仅限于idname)
  • 在我的回复中没有得到不必要的字段(例如__meta____owner__)

这是我的Controller:

# Controller
def index(conn, _) do
    subjects = Subject |> Repo.all
    conn |> render subjects: subjects
end
Run Code Online (Sandbox Code Playgroud)

这是我的View:

# View
def render("index.json", %{subjects: subjects}) do
    subjects
end
Run Code Online (Sandbox Code Playgroud)

这是我的回答:

[
    {
        "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"
        }
    }
]
Run Code Online (Sandbox Code Playgroud)

Eri*_*son 12

将您的观点更改为:

def render("index.json", %{subjects: subjects}) do
  Enum.map(subjects, &Map.take(&1, [:id, :name]))
end
Run Code Online (Sandbox Code Playgroud)

此外,您还可以通过将控制器更改为以下命令,让Ecto返回字段子集:

def index(conn, _) do
  subjects = from(s in Subject, select: %{id: s.id, name: s.name}) |> Repo.all
  conn |> render subjects: subjects
end
Run Code Online (Sandbox Code Playgroud)

  • 将`Map.take/2`改为`Map.drop/2`.您可以在http://elixir-lang.org/docs/stable/elixir/找到所有地图功能. (2认同)