Natural keys are a nice part of the serialization/fixture engine in django.
The bad part is that natural keys are not part of model APIs and thus they requires manual work to be done:
- defining a custom model-manager with the
get_by_natural_key
method - defining the
get_natural_key
method in model
This is done without any assurance that the selected fields for natural key are unique for the model (because every piece of code should be handled manually).
Currently there is an app which tries to provide some ready-to-use code for this use case: django-natural-keys, which lacks support for partial unique constraints (aka: unique constraint with a condition) and is not very predictable when there are more than one unique field (also it brings as mandatory a dependency which is only used when interacting with rest framework).
I would like to know if there was a reason for not implementing the natural-key APIs in the base model?
I belive that it would be cool to have natural key handled by the Meta
attribute of models by creating the appropriated methods on the default manager and on model class.
I feel that since natural-keys are already part of django serialization mechanism this would be a nice improvement to django, and I would like to hear if there are other people who belive this could be a valid improvement.