Opened 8 years ago

Closed 8 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: UI/UX:


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 8 years ago by ubernostrum

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Isn't this what limit_choices_to already does?

comment:2 Changed 8 years ago by adrian

  • Resolution set to worksforme
  • Status changed from new to closed

This is what limit_choices_to already does.

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

  • Resolution worksforme deleted
  • Status changed from closed to reopened

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 8 years ago by dcramer

  • Component changed from Database wrapper to Admin 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 8 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 8 years ago by jkocherhans

  • Resolution set to duplicate
  • Status changed from reopened to closed

This is a duplicate of #2445.

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