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

标签 elixir phoenix-framework ecto

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

我可以做什么:

  • 仅获取特定属性(例如仅 idname )
  • 不要在我的回复中获取不必要的字段(例如 __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"
            }
        }
    ]
    

    最佳答案

    将您的 View 更改为:

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

    此外,您还可以通过将 Controller 更改为以下内容来要求 Ecto 返回字段的子集:
    def index(conn, _) do
      subjects = from(s in Subject, select: %{id: s.id, name: s.name}) |> Repo.all
      conn |> render subjects: subjects
    end
    

    关于elixir - 在 Phoenix 中使用 Ecto 获取数据时仅获取特定字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31255126/

    相关文章:

    elixir - Ecto 模式默认值不包含在变更集更改中

    postgresql - Elixir,postgresql 错误 : has invalid types for the connection

    struct - 如何使用 Json 对结构进行自定义编码?

    elixir - 有没有办法在 Elixir 中注释掉函数中的代码行

    elixir - 如何撤消混合 phoenix.gen.html?

    elixir - 使用 ilike 在 Ecto 中搜索部分字符串

    elixir - "dialyzer: Analysis failed with error.."(透析器错误?或错误使用 map 类型?)

    elixir - 使用 Elixir/Phoenix,由于对 phoenix.gen.resource 生成的 CRUD 进行严格验证,无法从 Web UI 创建包含空字段的记录

    elixir - Phoenix 框架 : Maliciously Reusing Session Cookie To Access Website

    json - Phoenix、Json 和 Unix 时间戳