Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

magic-removal: fixed #1409

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2410 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f0e185c762ebc8d10d9414169ed8a20359065416 1 parent 841a12f
Joseph Kocherhans jkocherhans authored
4 django/db/models/fields/__init__.py
View
@@ -163,8 +163,8 @@ def has_default(self):
def get_default(self):
"Returns the default value for this field."
if self.default != NOT_PROVIDED:
- if hasattr(self.default, '__get_value__'):
- return self.default.__get_value__()
+ if callable(self.default):
+ return self.default()
return self.default
if not self.empty_strings_allowed or self.null:
return None
0  tests/modeltests/field_defaults/__init__.py
View
No changes.
48 tests/modeltests/field_defaults/models.py
View
@@ -0,0 +1,48 @@
+"""
+XXX. Callable defaults
+
+???
+"""
+
+from django.db import models
+from datetime import datetime
+
+class Article(models.Model):
+ headline = models.CharField(maxlength=100, default='Default headline')
+ pub_date = models.DateTimeField(default = datetime.now)
+
+ def __repr__(self):
+ return self.headline
+
+API_TESTS = """
+>>> from datetime import datetime
+
+# No articles are in the system yet.
+>>> Article.objects.all()
+[]
+
+# Create an Article.
+>>> a = Article(id=None)
+
+# Grab the current datetime it should be very close to the default that just
+# got saved as a.pub_date
+>>> now = datetime.now()
+
+# Save it into the database. You have to call save() explicitly.
+>>> a.save()
+
+# Now it has an ID. Note it's a long integer, as designated by the trailing "L".
+>>> a.id
+1L
+
+# Access database columns via Python attributes.
+>>> a.headline
+'Default headline'
+
+# make sure the two dates are sufficiently close
+>>> d = now - a.pub_date
+>>> d.seconds < 5
+True
+
+
+"""
Please sign in to comment.
Something went wrong with that request. Please try again.