Django

Code

Changeset 3145

Show
Ignore:
Timestamp:
06/18/06 21:34:32 (2 years ago)
Author:
mtredinnick
Message:

Fixed #1683 -- Permit initialising models using settable properties as well as
field names.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/base.py

    r3130 r3145  
    108108                val = kwargs.pop(f.attname, f.get_default()) 
    109109                setattr(self, f.attname, val) 
     110        for prop in kwargs.keys(): 
     111            try: 
     112                if isinstance(getattr(self.__class__, prop), property): 
     113                    setattr(self, prop, kwargs.pop(prop)) 
     114            except AttributeError: 
     115                pass 
    110116        if kwargs: 
    111117            raise TypeError, "'%s' is an invalid keyword argument for this function" % kwargs.keys()[0] 
  • django/trunk/tests/modeltests/properties/models.py

    r3028 r3145  
    1313    def _get_full_name(self): 
    1414        return "%s %s" % (self.first_name, self.last_name) 
     15 
     16    def _set_full_name(self, combined_name): 
     17        self.first_name, self.last_name = combined_name.split(' ', 1) 
     18 
    1519    full_name = property(_get_full_name) 
     20 
     21    full_name_2 = property(_get_full_name, _set_full_name) 
    1622 
    1723API_TESTS = """ 
     
    2632    ... 
    2733AttributeError: can't set attribute 
     34 
     35# But "full_name_2" has, and it can be used to initialise the class. 
     36>>> a2 = Person(full_name_2 = 'Paul McCartney') 
     37>>> a2.save() 
     38>>> a2.first_name 
     39'Paul' 
    2840"""