mirror of
https://github.com/django/django.git
synced 2025-04-14 20:34:36 +00:00
magic-removal: fixed #1409
git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2410 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
841a12f98d
commit
f0e185c762
@ -163,8 +163,8 @@ class Field(object):
|
||||
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
Normal file
0
tests/modeltests/field_defaults/__init__.py
Normal file
48
tests/modeltests/field_defaults/models.py
Normal file
48
tests/modeltests/field_defaults/models.py
Normal file
@ -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
|
||||
|
||||
|
||||
"""
|
Loading…
x
Reference in New Issue
Block a user