Opened 10 years ago

Last modified 9 years ago

#23780 closed New feature

Easy to use natural keys from a tuple on meta — at Version 2

Reported by: Brian Faherty Owned by: nobody
Component: Core (Serialization) Version: dev
Severity: Normal Keywords:
Cc: Russell Keith-Magee Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Brian Faherty)

Having to implement two methods on two different objects in order to use natural keys is very cumbersome. I would think we could implement something on a models meta to take a tuple of field names for use in both the natural_key and get_by_natural_key methods. I implemented this in a way that would be overridable to keep compatible with the current methods but would allow for easy use in the future. Working code with this is here https://github.com/scrummyin/django/commit/7e562127c3a9610ae3ad9fab6855a005f4d4706f. Includes 6 more tests around a two new methods, with same name but on the model and manager. The old tests are still working with minimal changes. The minimal change is to use a new method to check for a natural_key instead of hasattr. The pull request branch has all

Change History (2)

comment:1 by Brian Faherty, 10 years ago

Sorry, I just realized I broke some other tests. After I broke them and took another look at them, I think I didn't break them, but there were already messed up. I have adjusted those tests for what I think is the proper use case. I believe in their original form the serializer output the models in the wrong order. I reorder the data in the test and got it to work. When I looked at the new order I realized that the new order reflected the decencies correctly. A membership had fk's to person and group and it should have come after those two objects. This is correctly reflected in the revised tests. With this git commit https://github.com/scrummyin/django/commit/ce6d437c72de478c681a934509a186cb242f84f7.

comment:2 by Brian Faherty, 9 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top