Document Functions
Here’s a list of functions supported by the BaseDocument class and related utilities in the provided script:
Core Functions
__init__(self, d)- Initialize a document instance.update(self, d)- Update multiple fields using a dictionary.update_if_missing(self, d)- Set default values for fields without existing values.get(self, key, filters=None, limit=None, default=None)- Retrieve a field's value or child table rows with optional filters.getone(self, key, filters=None)- Retrieve a single matching child document.set(self, key, value, as_value=False)- Set a field's value or initialize child tables.delete_key(self, key)- Delete a field from the document.append(self, key, value=None)- Add a new row to a child table.extend(self, key, value)- Add multiple rows to a child table.remove(self, doc)- Remove a child document from a child table.as_dict(self, ...)- Serialize the document as a dictionary.as_json(self)- Serialize the document as JSON.get_valid_dict(self, ...)- Retrieve a dictionary of valid fields for database insertion.get_table_field_doctype(self, fieldname)- Get the doctype of a child table.db_insert(self, ignore_if_duplicate=False)- Insert the document into the database.db_update(self)- Update the document in the database.db_update_all(self)- Update the document and all child documents in the database.get_db_value(self, key)- Retrieve a field's value from the database.get_parentfield_of_doctype(self, doctype)- Get the parent fieldname for a given child doctype.update_modified(self)- Update themodifiedtimestamp in the database.
Validation
_validate_length(self)- Validate length constraints for fields._validate_selects(self)- ValidateSelectfield values against options._validate_data_fields(self)- ValidateDatafields (e.g., email, URL)._validate_constants(self)- Validate fields marked as "set only once."_validate_update_after_submit(self)- Validate updates after document submission._validate_non_negative(self)- Ensure numeric fields are not negative._validate_code_fields(self)- Validate fields containing code (e.g., Python expressions)._validate_links(self)- Check for invalid or canceled links in linked fields._validate_mandatory(self)- Validate mandatory fields.
Document Metadata and Utility
is_new(self)- Check if the document is new.get_formatted(self, fieldname, ...)- Retrieve a formatted version of a field's value.precision(self, fieldname, parentfield=None)- Get precision for numeric fields.is_print_hide(self, fieldname, ...)- Check if a field is hidden for print.in_format_data(self, fieldname)- Check if a field is part of the print format data.init_child_tables(self)- Initialize child tables for the document.init_valid_columns(self)- Initialize valid columns with default values.get_valid_columns(self)- Retrieve a list of valid columns for the document.
Field and Link Management
set_fetch_from_value(self, doctype, df, values)- Set value for fields withfetch_fromproperty.get_invalid_links(self, is_submittable=False)- Retrieve invalid or canceled links.
Password Management
_save_passwords(self)- Save password field values securely.get_password(self, fieldname="password", raise_exception=True)- Retrieve a password securely.is_dummy_password(self, pwd)- Check if a password is a dummy (e.g.,*****).
Error Handling
show_unique_validation_message(self, e)- Show validation errors for unique constraints.throw_length_exceeded_error(self, df, max_length, value)- Raise an error for exceeded length constraints.
Miscellaneous
get_label_from_fieldname(self, fieldname)- Get the label for a fieldname.get_value(self, fieldname)- Get a value for a field, casting it appropriately.cast(self, value, df)- Cast a value based on field type._sanitize_content(self)- Sanitize HTML and email fields to prevent XSS._fix_numeric_types(self)- Ensure numeric fields have consistent types._extract_images_from_text_editor(self)- Extract images embedded inText Editorfields.reset_values_if_no_permlevel_access(self, ...)- Reset fields based on permission levels.
This comprehensive list covers all primary methods defined in the script for interacting with, validating, and managing documents in the Frappe framework.