Opened 17 years ago

Closed 17 years ago

#5093 closed (duplicate)

ForeignKey should have a constraint

Reported by: Poromenos <poromenos@…> Owned by: nobody
Component: contrib.admin Version: dev
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

Description

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 by James Bennett, 17 years ago

Isn't this what limit_choices_to already does?

comment:2 by Adrian Holovaty, 17 years ago

Resolution: worksforme
Status: newclosed

This is what limit_choices_to already does.

comment:3 by Poromenos <poromenos@…>, 17 years ago

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 by David Cramer, 17 years ago

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 by Poromenos, 17 years ago

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 by jkocherhans, 17 years ago

Resolution: duplicate
Status: reopenedclosed

This is a duplicate of #2445.

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