93 | | for f in self._meta.fields: |
94 | | if isinstance(f.rel, ManyToOneRel): |
95 | | try: |
96 | | # Assume object instance was passed in. |
97 | | rel_obj = kwargs.pop(f.name) |
98 | | except KeyError: |
| 94 | # there is a rather weird disparity here; if kwargs, it's set, then args overrides it. |
| 95 | # should be one or the other, don't duplicate the work |
| 96 | # the reason for the kwargs check is that standard iterator passes in by args, literally, |
| 97 | # the row- with this check, instantiation for iteration is 33% faster. |
| 98 | args_len = len(args) |
| 99 | if args_len > len(self._meta.fields): |
| 100 | # daft, but matches old exception sans the err msg. |
| 101 | raise IndexError("number of args exceeds number of fields") |
| 102 | |
| 103 | fields_iter = iter(self._meta.fields) |
| 104 | if not kwargs: |
| 105 | # ordering of the izip calls matter- izip throws StopIteration when an iter throws it |
| 106 | # meaning, if the first iter throws it, the second is *not* consumed from |
| 107 | # we rely on this, thus don't change the order without changing the logic. |
| 108 | for val, field in izip(args, fields_iter): |
| 109 | setattr(self, field.attname, val) |
| 110 | else: |
| 111 | # slower... |
| 112 | for val, field in izip(args, fields_iter): |
| 113 | setattr(self, field.attname, val) |
| 114 | kwargs.pop(field.name, None) |
| 115 | # maintain compatibility with existing calls, daft as it is. |
| 116 | if isinstance(field.rel, ManyToOneRel): |
| 117 | kwargs.pop(field.attname, None) |
| 118 | |
| 119 | # now we're left with the unprocessed fields that *must* come from keywords, or default. |
| 120 | |
| 121 | for field in fields_iter: |
| 122 | if kwargs: |
| 123 | if isinstance(field.rel, ManyToOneRel): |
111 | | val = getattr(rel_obj, f.rel.get_related_field().attname) |
112 | | except AttributeError: |
113 | | raise TypeError, "Invalid value: %r should be a %s instance, not a %s" % (f.name, f.rel.to, type(rel_obj)) |
114 | | setattr(self, f.attname, val) |
| 129 | # Object instance wasn't passed in -- must be an ID. |
| 130 | val = kwargs.pop(field.attname) |
| 131 | except KeyError: |
| 132 | val = field.get_default() |
| 133 | else: |
| 134 | # Object instance was passed in. |
| 135 | # Special case: You can pass in "None" for related objects if it's allowed. |
| 136 | if rel_obj is None and field.null: |
| 137 | val = None |
| 138 | else: |
| 139 | try: |
| 140 | val = getattr(rel_obj, field.rel.get_related_field().attname) |
| 141 | except AttributeError: |
| 142 | raise TypeError("Invalid value: %r should be a %s instance, not a %s" % |
| 143 | (field.name, field.rel.to, type(rel_obj))) |
| 144 | else: |
| 145 | val = kwargs.pop(field.attname, field.get_default()) |
116 | | val = kwargs.pop(f.attname, f.get_default()) |
117 | | setattr(self, f.attname, val) |
118 | | for prop in kwargs.keys(): |
119 | | try: |
120 | | if isinstance(getattr(self.__class__, prop), property): |
121 | | setattr(self, prop, kwargs.pop(prop)) |
122 | | except AttributeError: |
123 | | pass |
| 147 | val = field.get_default() |
| 148 | setattr(self, field.attname, val) |
| 149 | |
125 | | raise TypeError, "'%s' is an invalid keyword argument for this function" % kwargs.keys()[0] |
126 | | for i, arg in enumerate(args): |
127 | | setattr(self, self._meta.fields[i].attname, arg) |
| 151 | for prop in kwargs.keys(): |
| 152 | try: |
| 153 | if isinstance(getattr(self.__class__, prop), property): |
| 154 | setattr(self, prop, kwargs.pop(prop)) |
| 155 | except AttributeError: |
| 156 | pass |
| 157 | if kwargs: |
| 158 | raise TypeError, "'%s' is an invalid keyword argument for this function" % kwargs.keys()[0] |