-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Respect to_field property of ForeignKey relations #2435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
067360c
8263048
d47f29a
49ecaa7
6369f93
6007463
f808b6c
77eeefa
3dd5ddd
6ccc45e
64c49f8
a496c52
8096997
54e16f4
5ca9841
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -97,13 +97,18 @@ def _get_forward_relationships(opts): | |
| """ | ||
| forward_relations = OrderedDict() | ||
| for field in [field for field in opts.fields if field.serialize and field.rel]: | ||
| # For < django 1.6 | ||
| if hasattr(field, 'to_fields'): | ||
| to_field = field.to_fields[0] if len(field.to_fields) else None | ||
| else: | ||
| to_field = None | ||
|
|
||
| forward_relations[field.name] = RelationInfo( | ||
| model_field=field, | ||
| related=_resolve_model(field.rel.to), | ||
| to_many=False, | ||
| # to_fields is an array but django lets you only set one to_field | ||
| to_field=field.to_fields[0] if len(field.to_fields) else None, | ||
| to_field=to_field, | ||
| has_through_model=False | ||
| ) | ||
|
|
||
|
|
@@ -130,11 +135,17 @@ def _get_reverse_relationships(opts): | |
| reverse_relations = OrderedDict() | ||
| for relation in opts.get_all_related_objects(): | ||
| accessor_name = relation.get_accessor_name() | ||
| # For < django 1.6 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused by the We always wrap any version branching behavior in
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests where failing on Travis for django versions below 1.6 and after a quick search i couldnt find to_field there so i assumed it was introduced in Django 1.6. A little more searching shows that it was there but it is the other way around: https://github.com/django/django/blob/stable/1.4.x/django/db/models/fields/related.py That makes me also concerned about defaulting to index zero |
||
| if hasattr(relation.field, 'to_fields'): | ||
| to_field = relation.field.to_fields[0] if len(relation.field.to_fields) else None | ||
| else: | ||
| to_field = None | ||
|
|
||
| reverse_relations[accessor_name] = RelationInfo( | ||
| model_field=None, | ||
| related=relation.model, | ||
| to_many=relation.field.rel.multiple, | ||
| to_field=relation.field.to_fields[0] if len(relation.field.to_fields) else None, | ||
| to_field=to_field, | ||
| has_through_model=False | ||
| ) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not that familiar with
to_fields, why are we only using the first index here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think theres a comment somewhere. Theres an array named to_fields which contains the to_field parameter as the first index. I don't know why they did this, maybe they planned to expand on that feature or had to remain backward compatible but this shows some code from the ForeignKey Class:
The documentation only refers to to_field and not to_fields.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'll might be worth linking to the relevant part of the Django source given that it's non-obvious.