Documentation on virtual docfields

Hi,

What’s the right way to create a virtual docfield with the Options field?

The documentation
https://frappeframework.com/docs/v14/user/en/basics/virtual_docfield#2-define-a-source-for-the-field
bullet two says

Using the DocField.options
[...]
frappe.utils.now_datetime() - self.creation

The above mentioned Person.full_name example uses Python's f-string feature to achive this in a similar way.

The example uses self to refer to the document
Just above it there is a screenshot which reads f"{doc.first_name}" so it uses doc instead of self
When saving a doctype with a virtual field with either doc or self, Frappe says {self.first_name} is not a valid fieldname pattern. It should be {field_name}.
And in the end Frappe still tries to read the field from the database, saying pymysql.err.OperationalError: (1054, "Unknown column '....full_name' in 'field list'")

:person_shrugging:

Use this syntax …

f'{doc.field1} - {doc.field2}'

It’s working. (Note that you can’t use this field on listview and many other situations)

Maybe screenshot is in the wrong place at document.

Hope this helps.

This is a message I get from Frappe while saving a doctype with such field:

edit:
@avc thanks1
doc.* works, as well as self.*, unless you try using the field as title, in which case it expects {fieldname} notation.

1 Like