#25306: Allow a limit_choices_to callable to accept the current model instance
------------------------------+------------------------------------
Reporter: Miles Hutson | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+------------------------------------
Comment (by Sven R. Kunze):
Also came across this issue recently.
A bunch of StackOverflow questions could be solved a lot easier when the
current instance would be available in ```limit_choices_to```:
https://stackoverflow.com/questions/tagged/limit-choices-to?tab=Active
https://stackoverflow.com/questions/39593577/how-to-get-instance-of-
entity-in-limit-choices-to-django
https://stackoverflow.com/questions/232435/how-do-i-restrict-foreign-keys-
choices-to-related-objects-only-in-django
https://stackoverflow.com/questions/47774013/django-limit-choices-to-to-
current-objects-id
https://stackoverflow.com/questions/28901089/use-field-value-in-limit-
choices-to-in-django
Having a db model solution would make the proposed work-arounds
obsolete/easier (especially they usually only work for a single form or
admin/inline). Also maintaining a single place of the choice restriction
is better also in terms of security (hidden/filtered choices due to
perms).
I would imagine two possible some solutions:
- one using an optional ```request=None, obj=None``` parameters to the
callable (backwards compatible)
- one using some special ```InstanceRef()``` object in a Q object
(analogously to ```OuterRef("pk")```
Personally, I would prefer the first solution because it's much more
flexible and permission-wise the Request object is sometimes needed to
realize more complex filtering.
@Simon nice lib 👍
--
Ticket URL: <https://code.djangoproject.com/ticket/25306#comment:8>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107019026858c9c-61d136d3-c9a0-4e71-b060-18d2914ffc4d-000000%40eu-central-1.amazonses.com.