Next step would be adding methods and event handlers to models. In the app, we should ensure that if a Library Transaction is made, the Article in question must be in stock and the member loaning the Article must have a valid membership.

For this, we can write a validation just before the Library Transaction object is saved. To do this, open the library_management/doctype/library_transaction/ template.

This file is the controller for the Library Transaction object. In this you can write methods for:

  1. before_insert
  2. validate (before inserting or updating)
  3. on_update (after saving)
  4. on_submit (when document is set as submitted)
  5. on_cancel
  6. on_trash (before it is about to be deleted)

You can write methods for these events and they will be called by the framework when the document is saved etc.

Here is the finished controller:

from __future__ import unicode_literals
import frappe
from frappe import _
from frappe.model.document import Document

class LibraryTransaction(Document):
    def validate(self):
        last_transaction = frappe.get_list("Library Transaction",
            fields=["transaction_type", "transaction_date"],
            filters = {
                "article": self.article,
                "transaction_date": ("<=", self.transaction_date),
                "name": ("!=",
        if self.transaction_type=="Issue":
            msg = _("Article {0} {1} has not been recorded as returned since {2}")
            if last_transaction and last_transaction[0].transaction_type=="Issue":
                frappe.throw(msg.format(self.article, self.article_name,
            if not last_transaction or last_transaction[0].transaction_type!="Issue":
                frappe.throw(_("Cannot return article not issued"))

In this script:

  1. We get the last transaction before the current transaction date using the query function frappe.get_list
  2. If the last transaction is something we don't like we throw an exception using frappe.throw
  3. We use _("text") method to identify translatable strings.

Check if your validations work by creating new records



To Debug, always keep your JS Console open. Lookout for both Javascript and server tracebacks.

Also check your terminal window for exceptions. Any 500 Internal Server Errors will get printed in your terminal where on which your server is running.

Next: Reports

On this page