summaryrefslogtreecommitdiff
path: root/tests/model_forms/models.py
diff options
context:
space:
mode:
authorChristopher Adams <christopher.r.adams@gmail.com>2014-02-01 14:23:31 -0500
committerTim Graham <timograham@gmail.com>2014-02-11 14:05:12 -0500
commiteefc88feefec0c3685bfb102714530b751b4ae90 (patch)
tree3cab4b02c13b76b6355d475d91ca2e157a126b18 /tests/model_forms/models.py
parenta718fcf201b04ba254e9073be82f51ae1ae3a853 (diff)
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.
Diffstat (limited to 'tests/model_forms/models.py')
-rw-r--r--tests/model_forms/models.py20
1 files changed, 19 insertions, 1 deletions
diff --git a/tests/model_forms/models.py b/tests/model_forms/models.py
index 72bf1ff26a..2b83a179ed 100644
--- a/tests/model_forms/models.py
+++ b/tests/model_forms/models.py
@@ -8,6 +8,7 @@ words, most of these tests should be rewritten.
"""
from __future__ import unicode_literals
+import datetime
import os
import tempfile
@@ -71,7 +72,6 @@ class Article(models.Model):
status = models.PositiveIntegerField(choices=ARTICLE_STATUS, blank=True, null=True)
def save(self):
- import datetime
if not self.id:
self.created = datetime.date.today()
return super(Article, self).save()
@@ -329,3 +329,21 @@ class CustomErrorMessage(models.Model):
def clean(self):
if self.name1 == 'FORBIDDEN_VALUE':
raise ValidationError({'name1': [ValidationError('Model.clean() error messages.')]})
+
+
+def today_callable_dict():
+ return {"last_action__gte": datetime.datetime.today()}
+
+
+def today_callable_q():
+ return models.Q(last_action__gte=datetime.datetime.today())
+
+
+class Character(models.Model):
+ username = models.CharField(max_length=100)
+ last_action = models.DateTimeField()
+
+
+class StumpJoke(models.Model):
+ 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="+")