Opened 14 years ago
Closed 11 years ago
#14549 closed New feature (fixed)
Disambiguation of target/source fields for intermediary models when using ManyToManyField
Reported by: | German M. Bravo | Owned by: | Akis Kesoglou |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | German M. Bravo, akiskesoglou@… | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
In cases where one has more than one foreign keys to either or both the source or the target models in a intermediary "through" model when using a ManyToManyField, django raises a validation error stating the ambiguity of the situation. To disambiguate this, I suggest adding two optional arguments to ManyToManyFields for those cases: 'source_fk_name' and 'target_fk_name'. I'm attaching a patch with an implementation of my suggestion.
Attachments (4)
Change History (19)
by , 14 years ago
Attachment: | #14549-intermediary_m2m_disambiguation.diff added |
---|
comment:1 by , 14 years ago
Needs tests: | set |
---|---|
Triage Stage: | Unreviewed → Design decision needed |
comment:2 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → New feature |
comment:3 by , 13 years ago
Easy pickings: | unset |
---|---|
UI/UX: | unset |
I have fixed the error in the tests (it was only an error due the new messages) and improved the patch a little, cleaning the messages a bit, fixing the wording and completing the help. It also now validates the case of many-to-many relationships to itself. Perhaps it needs a test where it actually uses the source_fk_name and target_fk_name arguments... but I'm not too sure on how to go about adding such test. I'm attaching the file now.
comment:4 by , 13 years ago
Cc: | added |
---|
by , 13 years ago
Attachment: | #14549-intermediary_m2m_disambiguation_fixed_tests.diff added |
---|
comment:5 by , 12 years ago
Triage Stage: | Design decision needed → Accepted |
---|
comment:6 by , 11 years ago
I'm attaching an updated patch that works on master and a test project with extra tests that prove it works. I haven't written any documentation.
All tests pass (i.e. both extras and Django's existing tests).
After loic84's suggestion in IRC, I implemented this feature as a through_fields=('<source_field_name>', '<target_field_name>')
keyword argument.
I'd love to have feedback and see the feature eventually added to Django.
by , 11 years ago
Attachment: | patch_commit_00e95a65d316.patch added |
---|
updated patch for django@9db4271bd11ac23a5a565
comment:7 by , 11 years ago
It's worth noting that I suggested through_fields=tuple()
before knowing and reviewing the existing patch; before we move forward, we need to make a call on the public API.
Personally I like through_fields
better, my rationale being:
- The
through_
prefix makes it obvious it's an option that is only available to customthrough
relations. - IMO what is "source" and "target" doesn't always make sense for M2M (i.e.
symmetrical
and relations toself
). In that respect, I would document it asthrough_fields=('<field1>', '<field2>')
and leave the concept of "source" and "target" out of the equation. - I prefer being fully explicit, rather than having a fallback mechanism for only one of the FK.
@dfunckt, would you mind integrating your tests directly in the Django test suite (probably in django/tests/many_to_many/tests.py
) rather than in a separate test project? Also it would be helpful if you made a github feature branch against Django's master (https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/working-with-git/), that would facilitate the review process.
comment:8 by , 11 years ago
Cc: | added |
---|
comment:9 by , 11 years ago
Cc: | added |
---|---|
Needs tests: | unset |
Added tests, documentation and cleaned up the code a bit. Just made a pull request: https://github.com/django/django/pull/2327
comment:10 by , 11 years ago
For this patch to go forward, is it possible that a core dev can confirm or discuss the API?
I personally agree with loic84 regarding the API which I believe is short, simple and explicit. However, I'm all for re-implementing the patch if the suggested API is not accepted and there are other suggestions.
comment:11 by , 11 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:12 by , 11 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
I'm marking as RTC as suggested by akaariai, after review.
comment:13 by , 11 years ago
Triage Stage: | Ready for checkin → Accepted |
---|
I'm removing the RFC flag, since we're currently trying to improve the docs a bit, along with loic84.
comment:14 by , 11 years ago
Cc: | removed |
---|---|
Triage Stage: | Accepted → Ready for checkin |
Looks good to me, tested, flake8ed, has lots of docs and tests.
comment:15 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
This needs tests and is currently causing a test error precisely in the model validation tests (modeltests.invalid_models):