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:


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.

Thanks, that worked… I had to find how to add it to my code. This helped:

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