mirror of
https://github.com/django/django.git
synced 2025-08-01 09:32:50 +00:00
Fixed #2445 -- Allowed limit_choices_to attribute to be a callable.
ForeignKey or ManyToManyField attribute ``limit_choices_to`` can now be a callable that returns either a ``Q`` object or a dict. Thanks michael at actrix.gen.nz for the original suggestion.
This commit is contained in:
parent
a718fcf201
commit
eefc88feef
15 changed files with 228 additions and 29 deletions
|
@ -173,6 +173,33 @@ class Sketch(models.Model):
|
|||
return self.title
|
||||
|
||||
|
||||
def today_callable_dict():
|
||||
return {"last_action__gte": datetime.datetime.today()}
|
||||
|
||||
|
||||
def today_callable_q():
|
||||
return models.Q(last_action__gte=datetime.datetime.today())
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Character(models.Model):
|
||||
username = models.CharField(max_length=100)
|
||||
last_action = models.DateTimeField()
|
||||
|
||||
def __str__(self):
|
||||
return self.username
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class StumpJoke(models.Model):
|
||||
variation = models.CharField(max_length=100)
|
||||
most_recently_fooled = models.ForeignKey(Character, limit_choices_to=today_callable_dict, related_name="+")
|
||||
has_fooled_today = models.ManyToManyField(Character, limit_choices_to=today_callable_q, related_name="+")
|
||||
|
||||
def __str__(self):
|
||||
return self.variation
|
||||
|
||||
|
||||
class Fabric(models.Model):
|
||||
NG_CHOICES = (
|
||||
('Textured', (
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue