Is there a way to test for uniqueness of a field through a custom function?
Something like:
def custom_unique_test(instance):
return global_test_results(instance)
class Category(models.Model)
slug = models.SlugField(unique=custom_unique_test())
Thanks
I’m assuming that your use case is that you only care about uniqueness within some relation with a larger group – eg, you don’t need globally unique slugs, so
unique=Trueis “too unique”, you only need unique slugs for eachCategorywithin aCategoryGroup.I’d suggest overriding
Category.save(). You can checkself.pkto see if this is an insert or update, and if it’s insert, you can call your custom unique-check and slug-generation code before callingsuper(Category, self).save(*args, **kwargs).