summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2005-08-01 16:26:39 +0000
committerAdrian Holovaty <adrian@holovaty.com>2005-08-01 16:26:39 +0000
commitb307fb09bb615361ee296d0c1f8d1a7c4809f6dd (patch)
treedcd24dd3208358dcdf774cce7367f5ee9528d49b
parent1a8fc57bf6e4b86fe047d04fea2efe50296634d0 (diff)
Fixed #239 and #107 -- Changed model init() to use Field.get_default() if the value wasn't explicitly passed as a keyword argument. That means setting 'id=None' is no longer necessary, and you can leave off fields if you want them to have default values set.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@360 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/core/meta.py11
-rw-r--r--docs/db-api.txt6
-rw-r--r--docs/overview.txt4
-rw-r--r--docs/tutorial01.txt2
-rw-r--r--tests/testapp/models/basic.py54
5 files changed, 62 insertions, 15 deletions
diff --git a/django/core/meta.py b/django/core/meta.py
index a6ef27a526..98ce768f72 100644
--- a/django/core/meta.py
+++ b/django/core/meta.py
@@ -720,14 +720,13 @@ class Model:
# CORE METHODS #############################
def method_init(opts, self, *args, **kwargs):
+ if kwargs:
+ for f in opts.fields:
+ setattr(self, f.name, kwargs.pop(f.name, f.get_default()))
+ if kwargs:
+ raise TypeError, "'%s' is an invalid keyword argument for this function" % kwargs.keys()[0]
for i, arg in enumerate(args):
setattr(self, opts.fields[i].name, arg)
- for k, v in kwargs.items():
- try:
- opts.get_field(k, many_to_many=False)
- except FieldDoesNotExist:
- raise TypeError, "'%s' is an invalid keyword argument for this function" % k
- setattr(self, k, v)
def method_eq(opts, self, other):
return isinstance(other, self.__class__) and getattr(self, opts.pk.name) == getattr(other, opts.pk.name)
diff --git a/docs/db-api.txt b/docs/db-api.txt
index 91ce2e8ac2..8a85c4ebdd 100644
--- a/docs/db-api.txt
+++ b/docs/db-api.txt
@@ -333,8 +333,7 @@ Creating new objects
Creating new objects (i.e. ``INSERT``) is done by creating new instances
of objects then calling save() on them::
- >>> p = polls.Poll(id=None,
- ... slug="eggs",
+ >>> p = polls.Poll(slug="eggs",
... question="How do you like your eggs?",
... pub_date=datetime.datetime.now(),
... expire_date=some_future_date)
@@ -355,8 +354,7 @@ Related objects (i.e. ``Choices``) are created using convience functions::
Each of those ``add_choice`` methods is equivilent to (except obviously much
simpler than)::
- >>> c = polls.Choice(id=None,
- ... poll_id=p.id,
+ >>> c = polls.Choice(poll_id=p.id,
... choice="Over easy",
... votes=0)
>>> c.save()
diff --git a/docs/overview.txt b/docs/overview.txt
index 7fba5e1767..19d48ba059 100644
--- a/docs/overview.txt
+++ b/docs/overview.txt
@@ -64,7 +64,7 @@ is created on the fly: No code generation necessary::
[]
# Create a new Reporter.
- >>> r = reporters.Reporter(id=None, full_name='John Smith')
+ >>> r = reporters.Reporter(full_name='John Smith')
# Save the object into the database. You have to call save() explicitly.
>>> r.save()
@@ -101,7 +101,7 @@ is created on the fly: No code generation necessary::
# Create an article.
>>> from datetime import datetime
- >>> a = articles.Article(id=None, pub_date=datetime.now(), headline='Django is cool', article='Yeah.', reporter_id=1)
+ >>> a = articles.Article(pub_date=datetime.now(), headline='Django is cool', article='Yeah.', reporter_id=1)
>>> a.save()
# Now the article is in the database.
diff --git a/docs/tutorial01.txt b/docs/tutorial01.txt
index bfdad7dfed..60f370e5a9 100644
--- a/docs/tutorial01.txt
+++ b/docs/tutorial01.txt
@@ -298,7 +298,7 @@ free Python API Django gives you::
# Create a new Poll.
>>> from datetime import datetime
- >>> p = polls.Poll(id=None, question="What's up?", pub_date=datetime.now())
+ >>> p = polls.Poll(question="What's up?", pub_date=datetime.now())
# Save the object into the database. You have to call save() explicitly.
>>> p.save()
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'
"""