How to set unique_together on DocType fields?

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?

Unfortunately not with standard functionality, had to write custom validation on saving a DocType. I’m willing to start development for this though.

Another way is to create a naming expression that involves the field names

Found this:
https://frappeframework.com/docs/v13/user/en/api/database#frappedbadd_unique

3 Likes

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.