Hi!

In the course of implementing `natural_key` for many different models, I've
noticed that the implementation is fairly predictable; it tends to use the
fields already marked as unique. To avoid writing a separate implementation
for each model, I've written a patch that implements the relevant logic on
the model and manager base classes:

https://github.com/jianli/django/compare/default-natural-key-implementation
https://github.com/jianli/django/commit/b6d644b45c379cae83f7f2609525e616b62ade52


Details:

- The proposed implementation is recursive, which enables it to create
natural foreign keys even when the foreign key structure is arbitrarily
deep.

- When a natural key is not available but the user specifies
`--natural-foreign`, the proposed implementation will raise an exception.
This is inconsistent with the current behavior of silently falling back to
using the "artificial" foreign key. However, I would argue that this is the
correct behavior, and also the behavior implied by the documentation; if
the user wants a natural foreign key serialization and Django is not able
to provide it, Django should let the user know with an exception.

- I was unsure whether to add the model method to `ModelBase` or `Model`.

- I've manually tested a nearly identical patch against 1.5.4, but haven't
had a chance to test against the development version.

Please let me know what you think, and keep up the amazing work!

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CACwUGkzF7yrR30maQbA1602dLgWsqqS%2BH%2Bew-EhzjkJ%3D90-JfA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to