Django

Code

Changeset 360

Show
Ignore:
Timestamp:
08/01/05 11:26:39 (3 years ago)
Author:
adrian
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/meta.py

    r359 r360  
    721721 
    722722def 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] 
    723728    for i, arg in enumerate(args): 
    724729        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" % k 
    730         setattr(self, k, v) 
    731730 
    732731def method_eq(opts, self, other): 
  • django/trunk/docs/db-api.txt

    r318 r360  
    334334of objects then calling save() on them:: 
    335335 
    336     >>> p = polls.Poll(id=None, 
    337     ...                slug="eggs", 
     336    >>> p = polls.Poll(slug="eggs", 
    338337    ...                question="How do you like your eggs?", 
    339338    ...                pub_date=datetime.datetime.now(), 
     
    356355simpler than):: 
    357356 
    358     >>> c = polls.Choice(id=None, 
    359     ...                  poll_id=p.id, 
     357    >>> c = polls.Choice(poll_id=p.id, 
    360358    ...                  choice="Over easy", 
    361359    ...                  votes=0) 
  • django/trunk/docs/overview.txt

    r316 r360  
    6565 
    6666    # Create a new Reporter. 
    67     >>> r = reporters.Reporter(id=None, full_name='John Smith') 
     67    >>> r = reporters.Reporter(full_name='John Smith') 
    6868 
    6969    # Save the object into the database. You have to call save() explicitly. 
     
    102102    # Create an article. 
    103103    >>> 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) 
    105105    >>> a.save() 
    106106 
  • django/trunk/docs/tutorial01.txt

    r328 r360  
    299299    # Create a new Poll. 
    300300    >>> 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()) 
    302302 
    303303    # Save the object into the database. You have to call save() explicitly. 
  • django/trunk/tests/testapp/models/basic.py

    r343 r360  
    99class Article(meta.Model): 
    1010    fields = ( 
    11         meta.CharField('headline', maxlength=100), 
     11        meta.CharField('headline', maxlength=100, default='Default headline'), 
    1212        meta.DateTimeField('pub_date'), 
    1313    ) 
     
    2020# Create an Article. 
    2121>>> 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)) 
    2324 
    2425# Save it into the database. You have to call save() explicitly. 
     
    7172>>> a == b 
    7273True 
     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 
     802L 
     81>>> a2.headline 
     82'Second article' 
     83>>> a2.pub_date 
     84datetime.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 
     913L 
     92>>> a3.headline 
     93'Third article' 
     94>>> a3.pub_date 
     95datetime.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') 
     106Traceback (most recent call last): 
     107    ... 
     108TypeError: '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 
     1145L 
     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' 
    73123"""