Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #1683 -- Permit initialising models using settable properties a…

…s well as

field names.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@3145 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 1fc3b3229a9b137f05a0a49bdc726f8c91317174 1 parent 214d88c
Malcolm Tredinnick authored June 19, 2006
6  django/db/models/base.py
@@ -107,6 +107,12 @@ def __init__(self, *args, **kwargs):
107 107
             else:
108 108
                 val = kwargs.pop(f.attname, f.get_default())
109 109
                 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
110 116
         if kwargs:
111 117
             raise TypeError, "'%s' is an invalid keyword argument for this function" % kwargs.keys()[0]
112 118
         for i, arg in enumerate(args):
12  tests/modeltests/properties/models.py
@@ -12,8 +12,14 @@ class Person(models.Model):
12 12
 
13 13
     def _get_full_name(self):
14 14
         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
+
15 19
     full_name = property(_get_full_name)
16 20
 
  21
+    full_name_2 = property(_get_full_name, _set_full_name)
  22
+
17 23
 API_TESTS = """
18 24
 >>> a = Person(first_name='John', last_name='Lennon')
19 25
 >>> a.save()
@@ -25,4 +31,10 @@ def _get_full_name(self):
25 31
 Traceback (most recent call last):
26 32
     ...
27 33
 AttributeError: 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'
28 40
 """

0 notes on commit 1fc3b32

Please sign in to comment.
Something went wrong with that request. Please try again.