Opened 11 years ago

Closed 11 years ago

#5093 closed (duplicate)

ForeignKey should have a constraint

Reported by: Poromenos <poromenos@…> Owned by: nobody
Component: contrib.admin Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Most of the time when you have a ForeignKey you only want it to access some of the table's data, so I propose a constrained be implemented (like the one for the admin site). The constraint would be specified in the model and would be a QuerySet object that filters objects from the table and returns the suitable ones as FK choices.

This would also work in forms, limiting their choices.

Change History (6)

comment:1 Changed 11 years ago by James Bennett

Isn't this what limit_choices_to already does?

comment:2 Changed 11 years ago by Adrian Holovaty

Resolution: worksforme
Status: newclosed

This is what limit_choices_to already does.

comment:3 Changed 11 years ago by Poromenos <poromenos@…>

Resolution: worksforme
Status: closedreopened

I will take another stab at this. limit_choices_to is not evaluated at call time, so it isn't exactly the same. I have modelA and modelB that have foreign keys to modelC. I want to be able to add a FK in modelA to modelB that will only display the objects in modelB that point to the same modelC as modelA (evaluated at call time).

comment:4 Changed 11 years ago by David Cramer

Component: Database wrapperAdmin interface

That's something that would have to be done inside of Python, versus SQL. I think with newforms-admin being more seperated, this is something that should be pushed into there if its needed.

comment:5 Changed 11 years ago by Poromenos

Would that work for newforms as well as the admin interface? The important part is the newforms one, because it would simplify almost every form that includes a foreign key (most django sites that include forms are multi-user setups, so the users should only see the FK choices of their own account, this is an example of where that would be useful).

comment:6 Changed 11 years ago by jkocherhans

Resolution: duplicate
Status: reopenedclosed

This is a duplicate of #2445.

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