Coming from a Django background, I can set a unique constraint for combined fields, not just on a single field. For example:
class DocTypeName():
foreign_key_field = LinkToAnotherDocType()
local_property/field = DataField()
class Meta:
unique_together = ("foreign_key_field", "local_property")
Having a setup like that can ensure that If a record is entered as:
{
"foreign_key_field": "A",
"local_property": "N",
}
I can not have another entry with same values, even if other fields have different fields.
Is there a way to have such a constraint on a DocType in frappeframework?
1 Like
Did you find a way to achieve this?
redgren
February 12, 2022, 11:00am
3
Unfortunately not with standard functionality, had to write custom validation on saving a DocType. I’m willing to start development for this though.
redgren
February 12, 2022, 12:08pm
4
Another way is to create a naming expression that involves the field names
redgren
February 18, 2022, 7:24am
6
Tested, works. Got a few thoughts though. In the case that there were existing records with same “combined” keys like this, would this work? I think now, it’d be nice to just have a field where we could specify, both on DocType and Customize Form for combined keys, maybe even a table to add with their names.
adam26d
November 21, 2022, 6:51pm
7
Thanks, that worked… I had to find how to add it to my code. This helped:
# Copyright (c) 2015, Frappe Technologies and contributors
# License: MIT. See LICENSE
import frappe
from frappe.model.document import Document
class EmailGroupMember(Document):
def after_delete(self):
email_group = frappe.get_doc("Email Group", self.email_group)
email_group.update_total_subscribers()
def after_insert(self):
email_group = frappe.get_doc("Email Group", self.email_group)
email_group.update_total_subscribers()
def after_doctype_insert():
frappe.db.add_unique("Email Group Member", ("email_group", "email"))
1 Like
One simple approach, possible in many scenarios, is to have the combination or more fields set as the PRIMARY key, i.e. set Naming as Expression : {field1}-{field2}-{fieldN}
.
Of course, this will not work for all scenarios.
1 Like