You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

naming-and-linking.md 2.8 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # DocType Naming and Linking
  2. Then let us create the other DocType and save it too:
  3. 1. Library Member (First Name, Last Name, Email Address, Phone, Address)
  4. <img class="screenshot" alt="Doctype Saved" src="/docs/assets/img/naming_doctype.png">
  5. #### Naming of DocTypes
  6. DocTypes can be named in different ways:
  7. 1. Based on a field
  8. 1. Based on a series
  9. 1. By controller (code)
  10. 1. Prompt
  11. This can be set by entering the **Autoname** field. For controller, leave blank.
  12. > **Search Fields**: A DocType may be named on a series but it still needs to be searched by name. In our case, the Article will be searched by the title or the author name. So this can be entered in search field.
  13. <img class="screenshot" alt="Autonaming and Search Field" src="/docs/assets/img/autoname_and_search_field.png">
  14. #### Link and Select Fields
  15. Foreign keys are specified in Frappe as **Link** type fields. The target DocType must be mentioned in the Options text area.
  16. In our example, in the Library Transaction DocType, we have to link both the Library Member and the Article.
  17. **Note:** Remeber that Link fields are not automatically set as Foreign Keys in the MariaDB database, because that will implicitly index the column. This may not be optimum hence the Foreign Key validation is done by the Framework.
  18. <img class="screenshot" alt="Link Field" src="/docs/assets/img/link_field.png">
  19. For select fields, as we mentioned earlier, add the various options in the **Options** input box, each option on a new row.
  20. <img class="screenshot" alt="Select Field" src="/docs/assets/img/select_field.png">
  21. Similary complete making the other models.
  22. #### Linked Values
  23. A standard pattern is when you select an ID, say **Library Member** in **Library Membership**, then the Member's first and last names should be copied into relevant fields in the Library Membership Transaction.
  24. To do this, we can use Read Only fields and in options, we can set the the name of the link and the fieldname of the property we want to fetch. For this example in **Member First Name** we can set `library_member.first_name`
  25. <img class="screenshot" alt="Fetch values" src="/docs/assets/img/fetch.png">
  26. ### Complete the Models
  27. In the same way, you can complete all the models so that the final fields look like this:
  28. #### Article
  29. <img class="screenshot" alt="Article" src="/docs/assets/img/doctype_article.png">
  30. #### Library Member
  31. <img class="screenshot" alt="Library Member" src="/docs/assets/img/doctype_lib_member.png">
  32. #### Library Membership
  33. <img class="screenshot" alt="Library Membership" src="/docs/assets/img/doctype_lib_membership.png">
  34. #### Library Transaction
  35. <img class="screenshot" alt="Library Transaction" src="/docs/assets/img/doctype_lib_trans.png">
  36. > Make sure to give permissions to **Librarian** on each DocType
  37. {next}