Linking models using API

Hi there,

I’m trying to programmatically populate the CMS using the content management api.
Sadly I cannot seem to find any information on how to “link” records together.

A push in the right direction would be very much appreciated.

To elaborate my situation;

I have a model called “School” and a model called “Partnership”.
A “School” can have one partnership so I want to create a field on “School” called “partnership” and attach the correct partnership which was previously created.

So I’m assuming I need to 2 things

  • create the field but I cannot find what the type should be.
  • look up which record was previously created to get some internal ID to provide as value to the field.

hello @ramon.gebben welcome!

What about doing something like this:

dato.items.all({
  "filter[type]": "143869", // My school model ID
})
  .then((schools) => {
    schools.forEach((school) => {
      dato.items.find(school.partnership).then((partnership) => {
        console.log(school);
        console.log(partnership);
      })
    })
  })
  .catch((error) => {
    console.log(error);
  });

Or if you can use GraphQL you could do something like this:

{
  allSchools {
    title
    partnership {
      title
    }
  }
}

If you can I encourage you to use GraphQL for reading as it’s faster, you use less API calls and for joins it’s much easier to implement!

@mat_jack1 Thanks for your response. However I’m looking to create these entities not list them.
For listen and consuming these values we indeed intent to use the GraphQL interface because I could not find any documentation on how to manage(create and populate) using GraphQL.

After inspection of the network tab while creating the entities using the web interface I figured out which values I needed to pass and ended up with something like this:

const field = {
  label: 'Schools',
  apiKey: 'schoolsl',
  position: 10,
  localized: false,
  fieldType: 'link',
  validators: {
    item_item_type: {
      item_types: ['1569'], // School model ID of the Link type
    },
    required: {},
  }
};

await dato.fields.create('1234', field);

Not sure if this is the way to go but this worked for me. Any hints on how to improve this are more than welcome.

1 Like

that’s perfect, and also you are right about GraphQL it’s intended to be read-only