Changeset 360
- Timestamp:
- 08/01/05 11:26:39 (3 years ago)
- Files:
-
- django/trunk/django/core/meta.py (modified) (1 diff)
- django/trunk/docs/db-api.txt (modified) (2 diffs)
- django/trunk/docs/overview.txt (modified) (2 diffs)
- django/trunk/docs/tutorial01.txt (modified) (1 diff)
- django/trunk/tests/testapp/models/basic.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/core/meta.py
r359 r360 721 721 722 722 def method_init(opts, self, *args, **kwargs): 723 if kwargs: 724 for f in opts.fields: 725 setattr(self, f.name, kwargs.pop(f.name, f.get_default())) 726 if kwargs: 727 raise TypeError, "'%s' is an invalid keyword argument for this function" % kwargs.keys()[0] 723 728 for i, arg in enumerate(args): 724 729 setattr(self, opts.fields[i].name, arg) 725 for k, v in kwargs.items():726 try:727 opts.get_field(k, many_to_many=False)728 except FieldDoesNotExist:729 raise TypeError, "'%s' is an invalid keyword argument for this function" % k730 setattr(self, k, v)731 730 732 731 def method_eq(opts, self, other): django/trunk/docs/db-api.txt
r318 r360 334 334 of objects then calling save() on them:: 335 335 336 >>> p = polls.Poll(id=None, 337 ... slug="eggs", 336 >>> p = polls.Poll(slug="eggs", 338 337 ... question="How do you like your eggs?", 339 338 ... pub_date=datetime.datetime.now(), … … 356 355 simpler than):: 357 356 358 >>> c = polls.Choice(id=None, 359 ... poll_id=p.id, 357 >>> c = polls.Choice(poll_id=p.id, 360 358 ... choice="Over easy", 361 359 ... votes=0) django/trunk/docs/overview.txt
r316 r360 65 65 66 66 # Create a new Reporter. 67 >>> r = reporters.Reporter( id=None,full_name='John Smith')67 >>> r = reporters.Reporter(full_name='John Smith') 68 68 69 69 # Save the object into the database. You have to call save() explicitly. … … 102 102 # Create an article. 103 103 >>> from datetime import datetime 104 >>> a = articles.Article( id=None,pub_date=datetime.now(), headline='Django is cool', article='Yeah.', reporter_id=1)104 >>> a = articles.Article(pub_date=datetime.now(), headline='Django is cool', article='Yeah.', reporter_id=1) 105 105 >>> a.save() 106 106 django/trunk/docs/tutorial01.txt
r328 r360 299 299 # Create a new Poll. 300 300 >>> from datetime import datetime 301 >>> p = polls.Poll( id=None,question="What's up?", pub_date=datetime.now())301 >>> p = polls.Poll(question="What's up?", pub_date=datetime.now()) 302 302 303 303 # Save the object into the database. You have to call save() explicitly. django/trunk/tests/testapp/models/basic.py
r343 r360 9 9 class Article(meta.Model): 10 10 fields = ( 11 meta.CharField('headline', maxlength=100 ),11 meta.CharField('headline', maxlength=100, default='Default headline'), 12 12 meta.DateTimeField('pub_date'), 13 13 ) … … 20 20 # Create an Article. 21 21 >>> from datetime import datetime 22 >>> a = articles.Article(id=None, headline='Area man programs in Python', pub_date=datetime(2005, 7, 28)) 22 >>> a = articles.Article(id=None, headline='Area man programs in Python', 23 ... pub_date=datetime(2005, 7, 28)) 23 24 24 25 # Save it into the database. You have to call save() explicitly. … … 71 72 >>> a == b 72 73 True 74 75 # You can initialize a model instance using positional arguments, which should 76 # match the field order as defined in the model... 77 >>> a2 = articles.Article(None, 'Second article', datetime(2005, 7, 29)) 78 >>> a2.save() 79 >>> a2.id 80 2L 81 >>> a2.headline 82 'Second article' 83 >>> a2.pub_date 84 datetime.datetime(2005, 7, 29, 0, 0) 85 86 # ...or, you can use keyword arguments. 87 >>> a3 = articles.Article(id=None, headline='Third article', 88 ... pub_date=datetime(2005, 7, 30)) 89 >>> a3.save() 90 >>> a3.id 91 3L 92 >>> a3.headline 93 'Third article' 94 >>> a3.pub_date 95 datetime.datetime(2005, 7, 30, 0, 0) 96 97 # You can also mix and match position and keyword arguments, but be sure not to 98 # duplicate field information. 99 >>> a4 = articles.Article(None, 'Fourth article', pub_date=datetime(2005, 7, 31)) 100 >>> a4.save() 101 >>> a4.headline 102 'Fourth article' 103 104 # Don't use invalid keyword arguments. 105 >>> a5 = articles.Article(id=None, headline='Invalid', pub_date=datetime(2005, 7, 31), foo='bar') 106 Traceback (most recent call last): 107 ... 108 TypeError: 'foo' is an invalid keyword argument for this function 109 110 # You can leave off the ID. 111 >>> a5 = articles.Article(headline='Article 6', pub_date=datetime(2005, 7, 31)) 112 >>> a5.save() 113 >>> a5.id 114 5L 115 >>> a5.headline 116 'Article 6' 117 118 # If you leave off a field with "default" set, Django will use the default. 119 >>> a6 = articles.Article(pub_date=datetime(2005, 7, 31)) 120 >>> a6.save() 121 >>> a6.headline 122 'Default headline' 73 123 """
