Custom Doctype Naming Series Code Snippet

Sometimes the naming series options just aren’t that great. I wanted to bring some of the specific variables from the record into the title. Here’s what I came up with:

    def before_insert(self):
        # adds meaningful_name naming series
        name_key = self.var_1 + " - " + frappe.utils.data.formatdate(self.var_date) + " - " + self.var_2 + " - .###"
        self.meaningful_name = frappe.model.naming.make_autoname(name_key, self, self)

Link to my personal use case/ example.

Add a variable called meaningful_name, which should probably be hidden. Set the Auto Name in the Doctype to: field:meaningful_name

Have fun, don’t over do it; most of the naming series in ERPNext are just fine.

1 Like

Hello tmatteson,

Thank you very much for the reply but could you please give more detailed steps as I have a very little experience.

Thank a lot.

I will answer you in the other thread, what you’re asking is a bit of deviation from this technique and I want to keep this one clear.

The is a slight bug on the function.
You need to dd an extra parameter because on event it passes 2 events.
The second parameter is just the event name.

Hey @aldoblack, which part? make_autoname(name_key, self, self)?

In function before_insert(self) , should be before_insert(self, event_name) .

Like a JS event trigger ("onload", "validate" etc)? Those guys? This is (intended to be) python, I have no idea if it’ll run on the client side. Can you share your error? I’ve never had to pass before_insert a trigger before, but something may have changed.

I am trying to make a customized naming series that uses a select field as part of the naming series for the document. My initial step towards even getting a simple custom defined naming series have not worked.

I followed this post and this one regarding customized naming and have come up empty on even getting the first step of getting the hooks and python file to set a field value. Some of my initial problems were related to having the “custom” button checked in the Doctype editor. That seemed to make the Doctype ignore the python file altogether.

My hooks.py file calls my Doctype “Document” in the document events section of the hooks file:

doc_events = {
"Document": {
   "autoname": "qms.quality_management.doctype.document.document.before_insert"
}
 }

In my document.py file I have:

from frappe.model.naming import make_autoname

class Document(Document):

     def before_insert(self):              
	    name_key = "self.document_number + " - .###""         
	    self.hidden_name = frappe.model.naming.make_autoname(name_key, self, self)

I also tried the other variation of this naming:

def item_autoname(doc, method):
    from frappe.model.naming import make_autoname
    doc.hidden_name = make_autoname(doc.document_number + '-.#####')

Could someone please guide me as to why my code is not setting a custom document name?

1 Like

Try running this with you doc_events in hooks commented out.
Also, naming your python method autoname will trigger it at the right time (before_insert).

def autoname(self):
    if self.document_number:
        self.name = make_autoname(self.document_number + '-.#####')

I commented out doc_events, played with different hooks calls and tried tmatteson’s function below without success.

def autoname(self):
    if self.document_number:
        self.name = make_autoname(self.document_number + '-.#####')

This is really stumping me. I created a brand new blank DocType to eliminate any other potential script conflicts to test that. @tmatteson, are you successfully using this autoname feature?

I am. Here’s an example from ERPNext:

You can do without the RegEx though, that must be for some kind of scrub.

Thank you for that reference code. I went line by line through the RestaurantTable.py code, the Doctype layouts, Doctype setups and confirmed that it works as intended. There is something in my custom app that seems to be preventing the autoname function from working. I am going to create a new droplet and see if autoname works in a fresh app environment and start tracing the problem from the ground up.

If you’re in developer mode it should pick the changes right up. It you’re in production mode, bench restart should work (but you should reconsider your dev workflow if you’re developing in production mode).

Eureka! Thank you @tmatteson. I owe you a case of New Hampshire’s finest brew. I was reloading the site in production mode instead of restarting the bench. Hard lesson learned but hopefully someone else will benefit from my wasted weekend struggling with this problem. Everything works even better than I had hoped since the autoname function tracks the numeric sequence for all my unique prefixes.

In full disclosure, I only know about this because I’ve made the same frustrating mistake.
I’ll still take your charity though: join the ERPNext Foundation if you haven’t already.