#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 Simon Charette):

 I thought I'd bring it the following since
 
[https://discord.com/channels/856567261900832808/1205482292580192367/1208377208780095558
 this was discussed on the Discord].

 About 15 years ago [https://github.com/charettes/django-dynamic-choices I
 created a now defunct third-party library that did exactly what is being
 asked for here], a way to have a callable receive the model instance to
 perform filtering against the available choices which confirms that this
 is technically doable.

 The interface was analogous to something like

 {{{#!python
 class Player(models.Model):
     name = models.CharField()
     team = models.ForeignKey('Team', limit_choices_to='allowed_teams')

     def allowed_teams(self, queryset):
         if self.captain_for:
             return queryset.filter(pk=self.captain_for.pk)
         return queryset

 class Team(models.Model):
     name = models.CharField()
     captain = models.OneToOneField(Player, related_name='captain_for')
 }}}

 That would generate the equivalent of

 {{{#!python
 class PlayerForm(ModelForm):
     class Meta:
        model = Player
        fields = '__all__'

     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
         if self.instance.pk and self.instance.captain_of:
             team_field = self.fields['team']
             team_field.queryset = team_field.queryset.filter(
                 pk=self.instance.captain_of
             )
 }}}

 But without the boilerplate.

 The library supported this feature but also a myriad of others that
 allowed for example to support the common country / state selection
 problem by hooking up views that would call into the backend to
 dynamically select fields.

 All to say that there might already be existing third packages
 implementing this feature if someone wants to tinker on how it can be
 done.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/25306#comment:7>
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/0107018db7f7db0c-9dfa6dbf-0def-4184-a264-42aa4852d419-000000%40eu-central-1.amazonses.com.

Reply via email to