2
0
mirror of https://github.com/frappe/books.git synced 2024-12-26 20:30:25 +00:00
books/docs/models/index.md

65 lines
1.4 KiB
Markdown
Raw Normal View History

2018-01-10 12:49:52 +00:00
# Declaring Models
2018-02-22 07:36:28 +00:00
Models are declared by adding a `.js` model file in the `models/doctype` folder of the module/app.
2018-01-10 12:49:52 +00:00
Note: A model is called `DocType` in Frappe.js
2018-02-22 07:36:28 +00:00
### Fields
Every model must have a set of fields (these become database columns). All fields may have the following properties:
2018-02-22 07:36:28 +00:00
- `fieldname`: Column name in database / property name.
- `fieldtype`: Data type ([see details](fields.md)).
- `label`: Display label.
- `required`: Is mandatory.
- `hidden`: Is hidden.
- `disabled`: Is disabled.
### Conditional Fields
The following fields: `hidden`, `required` can be conditional, depending on the values of other fields.
This is done by setting it's value to a function that receives an object with all the models fields and their values and returns a boolean.
See _"Posting Date"_ in the example below.
2018-02-22 07:36:28 +00:00
2018-01-10 12:49:52 +00:00
### Example
2018-02-22 07:36:28 +00:00
```js
module.exports = {
"naming": "random",
2018-01-10 12:49:52 +00:00
"name": "ToDo",
"doctype": "DocType",
"issingle": 0,
"fields": [
{
"fieldname": "subject",
"label": "Subject",
"fieldtype": "Data",
"required": 1
2018-01-10 12:49:52 +00:00
},
{
"fieldname": "description",
"label": "Description",
"fieldtype": "Text"
},
{
"fieldname": "status",
"label": "Status",
"fieldtype": "Select",
"options": [
"Open",
"Closed"
],
"default": "Open",
"required": 1
},
{
"fieldname": "postingDate",
"label": "Posting Date",
"fieldtype": "Date",
"required": (doc) => doc.status === "Closed"
},
2018-01-10 12:49:52 +00:00
]
}
```