Get parent field values or id from a child one in multiple link relation?

Hi. I have a question set model that has a question field which is of multiple link type. I want to access from a plugin in a question model the number of questions created so far under the question set, and also some field from the question set model (the parent). I would appreciate any help or hint as to how I should approach this.

@mat_jack1 is this something you can look into? This feature is really helpful for the product team. Thanks!

As you can see in the screenshot below, the “New Question” button is for creating a question model for the multiple link field within a question set model. Within my plugin for creating the question model, I want to access the ID of the parent question set record, since I wish to retrieve some fields from the parent in order to pre-populate certain fields of the question model. Inside my plugin’s component I can fetch all the question set records (all the possible parents) using the content management api, but I can’t tell to which specific parent (question set) my question model belongs to. I looked into the plugin but could not find anything that could point me to the parent. But when I inspect the response body of the api request made to create the question model, the parent Id is present as a relation. This tells me that the parent Id is available to the question model (maybe only internally), I am just not able to access it. I would appreciate any help that points me to some mechanism that exposes this parent Id within the question model. I want to access this info only when the question model is created from a multiple link field, not when it is created independently.

This is really crucial to avoid having to redundantly type some text inherited from the parent, into some field of the linked model, such as a question title which derives its title from the title of the parent question set.

hey @henok.tesfaye sorry for the delay here.

If you can see something interesting by inspecting the calls, you should be able to replay that call with our client and get back the same content. We are using the public APIs in the same way that you can do publicly. I hope this helps!

Otherwise maybe paste here which call you are seeing that returns the field that’s interesting to you and I’ll try replicating that with the JS client, OK?

Cheers

Hi @mat_jack1 thank you so much for looking into this.

This is the call that I inspected: https://site-api.datocms.com/editing-sessions/ckg0meusl00003h6aryw2aqv1. The request was made internally, when I click save in the panel that comes up after clicking “New Question”. I can not replicate the call, since I cannot provide the content in the request payload (see screenshot), such as the parent ID (8012194 is the parent Id in the screenshot). Even if I can replicate the call, it is not useful to me, since I don’t wish to create a new question by making an api call. I mentioned the request to highlight that parent ID (question set record Id) is internally accessible to the question model. I just want to know if that ID is accessible to me within the question model.

Hi @henok.tesfaye,
what you can do, using our Content Management API, is filter all the question sets that in the “questions” field include a specific question (in this case, question #7926456):

GET https://site-api.datocms.com/items?filter[type]=240299&filter[fields][questions][any_in][]=7926456

This call might do what you need for question sets that are already created, but of course it won’t work during the creation process of new questions, as the question itself still doesn’t exist, thus we don’t know its ID and it’s not yet linked to any question set.

May I add that, as in any database, I wouldn’t advise to denormalize data (copying data from the parent item into the child) in DatoCMS, as it could easily become stale/invalid. It would be better in your frontend to get the parent’s information you need… well, directly from the parent :slight_smile:

Is there anything that’s stopping you to do that? Why you need to denormalize data?

Maybe it will help if I tell you the scenario I am facing. I have a question set model and a question model. One question set may have hundreds of questions. And question sets have what is called a base title, for example one question set may have a base title of “Introduction to statistical thinking”. Now all the hundreds of questions that will be created in this question set will have this base title as part of their title, the question titles will be like: “Introduction to statistical thinking card 1”, “Introduction to statistical thinking card 2” … and so on. The product team are sick of having to type this base title again and again for each question. They are asking for ways to automate/pre-populate/ the question titles. So in order to automate this, I need two things. First be able to read the base title of the question set (the parent model) from a question model plugin. And second find out the number of question records created so far, so I can also pre-populate the part of the title that says “card 1”, “card 2”, and so on.

So this is really not a case of denormalization. I am just trying to automate what is otherwise done manually.

What you suggested me of filtering the question sets by question won’t work, since as you pointed the question is not created yet. But it should be possible to get the parent ID since the question is created as a multiple link field from a specific question set. I cannot find any parent field within a plugin, so I am hoping there might be some way of accessing at least the parent ID from within a plugin.

@s.verna thank you for your prompt response
And would appreciate further help on this

I see. No, unfortunately as of today the plugin does not expose the parent ID from which the form is being originated from.

Also, if you:

  • Click “Create new question set”
  • Click the “New question” to open up the modal

The parent “question set” still has no ID either, so that would be a problem in any case, isn’t it? :confused:

That is very unfortunate indeed :cry:. But since multiple links are expected to share a number of things, the need for automation should have been anticipated and the parent fields should have been exposed. I hope this is something that your team looks into so as to support it in upcoming versions.

And yeah of course I am not consecutively creating the question set and the question, since there would be nothing the questions share/inherit if the parent question set is not already created. The question set is already created and saved.

Thank you once again.

This issue is also present if the parent is already created, as it could have a number of non-saved changes (ie. other questions created but the parent question set still not saved), so simply exposing the parent ID would not be enough, we would also need to expose the pending changes of the parent record, which in turn could have validation errors… it’s quite a rabbit hole! And what about if you happen to reorder the questions?

Since it seems that every question is “${TITLE_OF_PARENT} #{INDEX}” I would still strongly suggest to calculate the question title on the frontend, and maybe tell the editors just to fill-in the title field if they want to override this default behaviour. This would solve your issue, right?

Anyway it is an interesting use case, one that we’ve not anticipated :slight_smile: I’m converting this topic into a future request, so that hopefully we can do something about it if there’s enough demand.

1 Like

In my case, there will not be any pending change in the parent. And all I need is the parent ID, since I can make an api request using that ID to get the parent fields I need. I do not get what you mean by reordering the questions.

And you are right, the question title can be calculated in the frontend. But the product team/the editors/ need to see the titles in dato as well. They simply cannot work with a blank title. They need to see the titles for all kinds of operations and processing they do with the questions, since I am dealing with a course website.

Thank you