How can I rename a document when saving it?

Hello Team, All,

I’m setting the name of a document as a combination of two of its fields. I would like to rename the document whenever the fields change. I’m trying the code below but it doesn’t quite help. Any assistance is welcome.

class Object(Document):
    def autoname(self):
        #Concatenate the academic year and the term name to form a unique name for combination of academic year/term = self.a + "/" + self.b
    def validate(self):
        #Add here the possibility to check if = self.academic_year + "/" + self.term_name
        #and rename if they are not the same
        new_name = self.a + "/" + self.b
        if != new_name:


1 Like

try using“/”+self.b)
for that you have to import
from frappe.model.naming import make_autoname
Ravindra Lakal

Hi @ravindra_l tried this but gives error “Doctype name not found”
also tried set_new_name(self) still gives the same error, I guess won’t work if you are in same window?


on first Validate it will give you = 'New Document Name 1' for New Document. So, you have to check on validate Document is New one or Existing one.

Try this,

def validate(self):
	new_name = self.a + "/" + self.b
	if != new_name and not self.is_new():
#import the function
import frappe.model.rename_doc as rd
rd.rename_doc(Doctype, old_name, new, force=False, merge=False, ignore_permissions=False, ignore_if_exists=False)

This works for me

1 Like

Where should I place this code?

1 Like

It will depend when and where you want to rename your document.

Right now I had a DocType in which the name is a combination of two fields, and I want it to keep in sync. So I do the following.

    def autoname(self):
        """If field is new sets the name, if fields that set
        the name have changed, renames"""
        # Get the name the record should have
        name = self.get_name()
        # If it doesn't exis, e.a Is a new record
        # Name it and end it
        if not frappe.db.exists('Package',
   = name
            # This will be called on_update, meaning if the name
            # Is not what It should be, rename
            if != self.get_name():
                        , name, ignore_permissions=True)

    def get_name(self):
        '''Method that returns the record name'''
        customer = self.customer
        if len(customer) > 3:
            customer = customer[0:3].upper()

        return f"{customer}-{}"

    def on_update(self):

It works this way:

When Document is new

  1. Fiels is new, calls the autoname controller.
  2. The controller check if the document exists already. If it doesn’t exist, is new, so sets the name.

When Document Exists

  1. call on_update, which call autoname.
  2. Since field already exists (the fact that on_update is called tells you the field exists). It checks if name is what It should be (given by get_name()) if not, rename.
1 Like

After frappe.rename_doc use frappe.db.commit

frappe.rename_doc(“Your Doctype”, “old_name”, “new_name”)