Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Admin: Can I use another field for <ReferenceInput>, other than "id"?

Is there any way to use a different reference field (other than id) for a ReferenceInput?

For example:
I have a languages resource, which I want to populate using a language_code field.
Note, I don't want to use the id field of languages, I want to use the code field.

I have managed to get it to work by using the following:

<ReferenceInput 
  label="Language code" 
  source="language_code" 
  reference="languages"
>
  <SelectInput optionText="name" optionValue="code" />
</ReferenceInput>

The only problem with this is that:

After selecting a language from the select input, the ReferenceInput tries to fetch the resource using the code field rather than the id field, which returns a 404 error.

like image 310
Ross Avatar asked Jun 25 '18 10:06

Ross


1 Answers

The value stored as the reference to the languages in the resource must be its identifier. Indeed, if you add a ReferenceField in the show view of your resource, how can it fetch the language without its id ?

You have two options here:

  • make your API support the languages/en route
  • store the language_id in addition to the language_code in your resource and create a custom ReferenceInput which will return both values after selection
like image 62
Gildas Garcia Avatar answered Nov 15 '22 08:11

Gildas Garcia