Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#26985 closed Cleanup/optimization (fixed)

Document restrictions on fields that can be referenced by ForeignKey.to_field

Reported by: Baptiste Mispelon Owned by: Jonatas CD
Component: Documentation Version: 1.10
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

As someone who's never used the feature before (I looked at the documentation while investigating something else), it's not clear to me if there are limitations or restrictions on what kind of fields can be used?

For example, does it need to be an IntegerField or can it be any field (trying it out, it seems that a CharField works without issues).

One restriction which is caught by Django's validation framework is that the field needs to have unique=True (this is the error fields.E311) but I don't know if there are more.

Change History (5)

comment:1 by Tim Graham, 8 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Jonatas CD, 8 years ago

Owner: changed from nobody to Jonatas CD
Status: newassigned

comment:3 by Jonatas CD, 8 years ago

Has patch: set

Based on a search into the code, you can find uses with CharField, EmailField, IntegerField, PositiveIntegerField, UUIDField.
But in all cases indeed the referenced field has to be unique=True. Apart from that I didn't see any other restriction.
Patch added updating documentation.

comment:4 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In 9da45ff:

Fixed #26985 -- Doc'd that ForeignKey.to_field reference must be unique.

comment:5 by Tim Graham <timograham@…>, 8 years ago

In af0f015:

[1.10.x] Fixed #26985 -- Doc'd that ForeignKey.to_field reference must be unique.

Backport of 9da45ff352db1d1b7c64e5c724e9b0b31822b967 from master

Note: See TracTickets for help on using tickets.
Back to Top