[Django] #22787: natural key serialization

12 views
Skip to first unread message

Django

unread,
Jun 7, 2014, 6:09:30 AM6/7/14
to django-...@googlegroups.com
#22787: natural key serialization
--------------------------------------+--------------------
Reporter: jian | Owner: jian
Type: New feature | Status: new
Component: Core (Serialization) | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Easy pickings: 0 | UI/UX: 0
--------------------------------------+--------------------
In order to use natural key serialization, the user has to define
`model.natural_key` and `manager.get_by_natural_key` methods for every
single model to be serialized. This is tedious and requires many LOC if
the user wants to serialize multiple models, especially if those models do
not already have custom manager classes.

To remedy this, the linked pull request defines a base class
implementation of `model.natural_key` and `manager.get_by_natural_key`.
This implementation automatically produces natural keys that uniquely
identify each model without recourse to the `pk` field, and the results
are qualitatively similar to what the user would have defined in custom
methods.

In the rare instance where the model lacks any uniqueness constraints,
this approach would not work, and invoking `natural_key` or
`get_by_natural_key` would raise a `NotImplementedError`. In such a
scenario, it is not clear that even a user-defined natural key would work
(but, as always, the user is free to implement one anyway).

Currently, if the user requests a natural key serialization, Django
introspects the model using `hasattr(model, 'natural_key')` and silently
falls back to standard serialization if a natural key serialization is not
available. Using `hasattr` is no longer possible with the new
implementation, so this pull request removes these checks, and Django will
instead complain with an informative message such as `CommandError: Unable
to serialize database: The Question class must provide a natural_key()
method`. This might be a backward-incompatible change, but it is the
correct one. When a natural key serialization is requested but
unavailable, the correct behavior is for Django to complain loudly rather
than being silent ("explicit is better than implicit").

The pull request includes a new regression test, and passes all tests
using SQLite version 3.7.13.

django-developers discussion: https://groups.google.com/d/msg/django-
developers/6ZboknjrSkg/vOQXDSDhG28J

--
Ticket URL: <https://code.djangoproject.com/ticket/22787>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 7, 2014, 6:15:02 AM6/7/14
to django-...@googlegroups.com
#22787: natural key serialization
-------------------------------------+-------------------------------------

Reporter: jian | Owner: jian
Type: New feature | Status: new
Component: Core | Version: master
(Serialization) | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jian):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

github pull request: https://github.com/django/django/pull/2771

--
Ticket URL: <https://code.djangoproject.com/ticket/22787#comment:1>

Django

unread,
Jun 24, 2014, 10:04:44 AM6/24/14
to django-...@googlegroups.com
#22787: Add a default implementation of Model.natural_key()
-------------------------------------+-------------------------------------

Reporter: jian | Owner: jian
Type: New feature | Status: new
Component: Core | Version: master
(Serialization) | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by timo):

Last status on this is that Russ was -0 on the mailing list, but hasn't
responded to Jian's follow-up.

--
Ticket URL: <https://code.djangoproject.com/ticket/22787#comment:2>

Django

unread,
Jun 24, 2014, 8:34:11 PM6/24/14
to django-...@googlegroups.com
#22787: Add a default implementation of Model.natural_key()
-------------------------------------+-------------------------------------
Reporter: jian | Owner: jian
Type: New feature | Status: new
Component: Core | Version: master
(Serialization) | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by russellm):

Just commented on the mailing list thread - I'm still -0.

--
Ticket URL: <https://code.djangoproject.com/ticket/22787#comment:3>

Django

unread,
Jul 4, 2014, 1:57:20 PM7/4/14
to django-...@googlegroups.com
#22787: Add a default implementation of Model.natural_key()
-------------------------------------+-------------------------------------
Reporter: jian | Owner: jian
Type: New feature | Status: closed
Component: Core | Version: master
(Serialization) | Resolution: wontfix

Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timo):

* status: new => closed
* resolution: => wontfix


Comment:

Reading the thread, I agree with Russ.

--
Ticket URL: <https://code.djangoproject.com/ticket/22787#comment:4>

Reply all
Reply to author
Forward
0 new messages