Opened 14 years ago
Closed 12 years ago
#13822 closed Bug (wontfix)
ForeignKey's attname is counter-intuitive when referencing to a field not named 'id'
Reported by: | Alexander Schepanovski | Owned by: | Alexander Schepanovski |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.2 |
Severity: | Normal | Keywords: | db, ForeignKey |
Cc: | Triage Stage: | Design decision needed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
class SomeModel(model.Model): region = models.ForeignKey(Region, to_field='code') sm = SomeModel() sm.region # works sm.region_code # AttributeError, currently sm.region_id # works, but evaluates to sm.region.code
Also, currently we have:
CREATE TABLE "app_somemodel" ( ... "region_id" integer NOT NULL REFERENCES "geo_region" ("code") DEFERRABLE INITIALLY DEFERRED, ... );
which also looks strange.
Attachments (1)
Change History (7)
by , 14 years ago
Attachment: | foreignkey_attname.diff added |
---|
comment:1 by , 14 years ago
comment:2 by , 14 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
Not *very* backward incompatable since references to fields named "id" (most primary keys) won't trigger changes.
Still it is backward incompatable, promoting to design decision needed.
comment:3 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → Bug |
comment:6 by , 12 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
I don't think there's much value in this change at this point in the history of Django.
Note:
See TracTickets
for help on using tickets.
First, this change is *very* backwards incompatible, as it would require to migrate any db schema that has a foreign key.
Second, I hope that someday Django will support multi-column (primary) keys and it probably won't look so good then.