Fixed #2445: Allow callable values for limit_choices_to. #1228

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
5 participants
Contributor

zyegfryed commented May 27, 2013

Introduces the Model.choices_for_FOO method allowing to extend the limit_choices_to option.

@charettes charettes and 1 other commented on an outdated diff May 27, 2013

docs/ref/models/instances.txt
+.. method:: Model.choices_for_FOO()
+
+ .. versionadded:: 1.6
+
+ For every :class:`~django.db.models.ForeignKey` which
+ :attr:`~django.db.models.ForeignKey.limit_choices_to` is too limited, you
+ can define a ``choices_for_FOO`` method, where ``FOO`` is the name of the
+ field, that returns a queryset. It allows you to limit choices with
+ respect to ``self``. For example::
+
+ class MyModel(models.Model):
+ user = models.ForeignKey(User)
+
+ def choices_for_user(self):
+ return self._default_manager.exclude(is_active=False) |
+ self._default_manager.filter(id=self.user)
@charettes

charettes May 27, 2013

Member

Isn't self an instance of MyModel here? Then shouldn't this return User.objects.filter(Q(id=self.user) | Q(is_active=True) instead?

@zyegfryed

zyegfryed May 27, 2013

Contributor

Good catch. Thanks.

@zyegfryed

zyegfryed May 27, 2013

Contributor

Fixed in the last commit.

@mjtamlyn mjtamlyn and 1 other commented on an outdated diff Jun 4, 2013

docs/ref/models/instances.txt
+
+.. method:: Model.choices_for_FOO()
+
+ .. versionadded:: 1.6
+
+ For every :class:`~django.db.models.ForeignKey` which
+ :attr:`~django.db.models.ForeignKey.limit_choices_to` is too limited, you
+ can define a ``choices_for_FOO`` method, where ``FOO`` is the name of the
+ field, that returns a queryset. It allows you to limit choices with
+ respect to ``self``. For example::
+
+ class MyModel(models.Model):
+ user = models.ForeignKey(User)
+
+ def choices_for_user(self):
+ return User.objects.filter(Q(id=self.user) | Q(is_active=True)
@mjtamlyn

mjtamlyn Jun 4, 2013

Member

Missing a closing ) here.

@zyegfryed

zyegfryed Jun 4, 2013

Contributor

Right. Thanks.

@bmispelon bmispelon and 2 others commented on an outdated diff Jun 4, 2013

django/db/models/fields/__init__.py
@@ -511,6 +506,13 @@ def _get_flatchoices(self):
return flat
flatchoices = property(_get_flatchoices)
@bmispelon

bmispelon Jun 4, 2013

Member

Since we don't have to support old python versions anymore, you can use the @property syntax, which I think is more readable.

@zyegfryed

zyegfryed Jun 4, 2013

Contributor

I was following the coding rules used in the module for the sake of consistency. Do you want me to use @property syntax all over the module and update old code too?

@mjtamlyn

mjtamlyn Jun 4, 2013

Member

May as well while we're here

@timgraham timgraham commented on an outdated diff Jun 4, 2013

docs/ref/models/instances.txt
+ respect to ``self``. For example::
+
+ class MyModel(models.Model):
+ user = models.ForeignKey(User)
+
+ def choices_for_user(self):
+ return User.objects.filter(Q(id=self.user) | Q(is_active=True))
+
+ will make only active and currently associated users choosable.
+
+ .. note::
+
+ The ``choices_for_FOO`` method takes precedence over the
+ :attr:`~django.db.models.ForeignKey.limit_choices_to` option. So, when
+ a ``choices_for_FOO`` method is available it will always be used
+ instead of the queryset generated against
@timgraham

timgraham Jun 4, 2013

Owner

against -> using

Owner

timgraham commented Jun 4, 2013

This also needs to be added to the release notes both as a minor feature and as a backwards incompatible change (for the rare chance that someone has a choices_for_FOO method already defined on his models).

zyegfryed Fixed #2445: Allow callable values for limit_choices_to.
Introduces the Model.choices_for_FOO method allowing to extend the limit_choices_to option.
Fixed legacy code to use the decorator syntax for property.
Added release notes and backward compatibility documentation.
Thanks @charettes, @evildmp, @mjtamlyn, @bmispelon and @timgraham for feedback.
22b7e59
Owner

timgraham commented Jun 6, 2013

Per comment on the ticket, this approach has been rejected in favor of passing choices a callable.

timgraham closed this Jun 6, 2013

zyegfryed deleted the unknown repository branch Jun 6, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment