I have been developing an independent app named Container Management. For this purpose, i have created a doctype named Container having three fields container_id, size and mt_date. I have written the following code in container.py to validate the data in the server-side -
for row in frappe.get_all("Container", fields = ["container_id", "size"]):
if len(row["container_id"]) != 11:
frappe.throw('Container Id should be 11 characters long !!')
if row["size"] != "20GP" or row["size"] != "40GP":
frappe.throw('Size should be 20-GP or 40-GP')
But when i reload in the app (before inputing any data) it throws the server error
I have written the code explicitly, not within a function. Here is the total code from the file -
# -*- coding: utf-8 -*-
# Copyright (c) 2015, Reza and contributors
# For license information, please see license.txt
from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
class Container(Document):
pass
for row in frappe.get_all("Container", fields = ["container_id", "size"]):
if len(row["container_id"]) != 11:
frappe.throw('Container Id should be 11 characters long !!')
if row["size"] != "20GP" or row["size"] != "40GP":
frappe.throw('Size should be 20-GP or 40-GP')
I have also guessed that the cause may be indentation, i have tried re-indentation, the result is same.
# -*- coding: utf-8 -*-
# Copyright (c) 2015, Reza and contributors
# For license information, please see license.txt
from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
class Container(Document):
def validate(self):
for row in frappe.get_all("Container", fields = ["container_id", "size"]):
if len(row["container_id"]) != 11:
frappe.throw('Container Id should be 11 characters long !!')
if row["size"] != "20GP" or row["size"] != "40GP":
frappe.throw('Size should be 20-GP or 40-GP')
Sorry for late reply. I was away from keyboard. And thank you very much for your suggestion . I have tried your suggestion, but still getting the same result. Is it have anything to do with cache ?
Another question, do i have to define methods inside Container class, everytime i am doing a query or adding validation ? Doesn’t code outside the class scope work?
Why are you using get_all()? I think you just want to validate the data in the current form. You code should be like this:
from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
class Container(Document):
def validate(self):
if len(self.container_id) != 11:
frappe.throw('Container Id should be 11 characters long !!')
if self.size != "20GP" or self.size != "40GP":
frappe.throw('Size should be 20-GP or 40-GP')