summaryrefslogtreecommitdiff
path: root/tests/testapp/models/basic.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/testapp/models/basic.py')
-rw-r--r--tests/testapp/models/basic.py54
1 files changed, 52 insertions, 2 deletions
diff --git a/tests/testapp/models/basic.py b/tests/testapp/models/basic.py
index 120c356765..86b08122c3 100644
--- a/tests/testapp/models/basic.py
+++ b/tests/testapp/models/basic.py
@@ -8,7 +8,7 @@ from django.core import meta
class Article(meta.Model):
fields = (
- meta.CharField('headline', maxlength=100),
+ meta.CharField('headline', maxlength=100, default='Default headline'),
meta.DateTimeField('pub_date'),
)
@@ -19,7 +19,8 @@ API_TESTS = """
# Create an Article.
>>> from datetime import datetime
->>> a = articles.Article(id=None, headline='Area man programs in Python', pub_date=datetime(2005, 7, 28))
+>>> a = articles.Article(id=None, headline='Area man programs in Python',
+... pub_date=datetime(2005, 7, 28))
# Save it into the database. You have to call save() explicitly.
>>> a.save()
@@ -70,4 +71,53 @@ ArticleDoesNotExist: Article does not exist for {'id__exact': 2}
>>> b = articles.get_object(pk=1)
>>> a == b
True
+
+# You can initialize a model instance using positional arguments, which should
+# match the field order as defined in the model...
+>>> a2 = articles.Article(None, 'Second article', datetime(2005, 7, 29))
+>>> a2.save()
+>>> a2.id
+2L
+>>> a2.headline
+'Second article'
+>>> a2.pub_date
+datetime.datetime(2005, 7, 29, 0, 0)
+
+# ...or, you can use keyword arguments.
+>>> a3 = articles.Article(id=None, headline='Third article',
+... pub_date=datetime(2005, 7, 30))
+>>> a3.save()
+>>> a3.id
+3L
+>>> a3.headline
+'Third article'
+>>> a3.pub_date
+datetime.datetime(2005, 7, 30, 0, 0)
+
+# You can also mix and match position and keyword arguments, but be sure not to
+# duplicate field information.
+>>> a4 = articles.Article(None, 'Fourth article', pub_date=datetime(2005, 7, 31))
+>>> a4.save()
+>>> a4.headline
+'Fourth article'
+
+# Don't use invalid keyword arguments.
+>>> a5 = articles.Article(id=None, headline='Invalid', pub_date=datetime(2005, 7, 31), foo='bar')
+Traceback (most recent call last):
+ ...
+TypeError: 'foo' is an invalid keyword argument for this function
+
+# You can leave off the ID.
+>>> a5 = articles.Article(headline='Article 6', pub_date=datetime(2005, 7, 31))
+>>> a5.save()
+>>> a5.id
+5L
+>>> a5.headline
+'Article 6'
+
+# If you leave off a field with "default" set, Django will use the default.
+>>> a6 = articles.Article(pub_date=datetime(2005, 7, 31))
+>>> a6.save()
+>>> a6.headline
+'Default headline'
"""