10 examples of 'django model to dict' in Python

Every line of 'django model to dict' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
298def model_to_dict(self):
299 """Returns a dict containing the data in ``instance`` suitable for
300 passing as forms ``initial`` keyword argument."""
301 opts = self._meta
302 return model_to_dict(self.instance, opts.fields, opts.exclude)
201def to_native(self, obj):
202 """
203 Rest framework built-in to_native + transform_object
204 """
205 ret = self._dict_class()
206 ret.fields = self._dict_class()
207
208 #Dynamic Document Support
209 dynamic_fields = self.get_dynamic_fields(obj)
210 all_fields = self._dict_class()
211 all_fields.update(self.fields)
212 all_fields.update(dynamic_fields)
213
214 for field_name, field in all_fields.items():
215 if field.read_only and obj is None:
216 continue
217 field.initialize(parent=self, field_name=field_name)
218 key = self.get_field_key(field_name)
219 value = field.field_to_native(obj, field_name)
220 #Override value with transform_ methods
221 method = getattr(self, 'transform_%s' % field_name, None)
222 if callable(method):
223 value = method(obj, value)
224 if not getattr(field, 'write_only', False):
225 ret[key] = value
226 ret.fields[key] = self.augment_field(field, field_name, key, value)
227
228 return ret
44def to_dict(self):
45 """Returns the model's attributes as a dictionary."""
46 result = {}
47 res = sqlalchemy.inspect(self)
48
49 for c in self.__table__.columns:
50 if c.key not in res.unloaded:
51 try:
52 result[c.name] = getattr(self, c.name)
53 except AttributeError:
54 pass
55
56 for r in self.__mapper__.relationships:
57 if r.key not in res.unloaded:
58 result[r.key] = getattr(self, r.key)
59
60 return result
277def as_dict(self):
278 """Return a dict of column attributes."""
279 return serialize(self)
314def to_native(self, obj=None):
315 if self.obj_is_sequence(obj):
316 self.cache_all()
317 return super(SimpleSerializerWithLookups, self).to_native(obj)
14def user_obj_to_django(user_obj):
15 c_attrs = settings.COGNITO_ATTR_MAPPING
16 user_attrs = dict()
17 for k,v in user_obj.__dict__.iteritems():
18 dk = c_attrs.get(k)
19 if dk:
20 user_attrs[dk] = v
21 return user_attrs
54def to_dict(model):
55 output = {}
56
57 for key, prop in model.properties().iteritems():
58 value = getattr(model, key)
59
60 if value is None or isinstance(value, SIMPLE_TYPES):
61 output[key] = value
62 elif isinstance(value, db.Model):
63 output[key] = to_dict(value)
64 else:
65 raise ValueError('cannot encode ' + repr(prop))
66
67
68 return output
120def to_dict(self):
121 res = {}
122 for name, field in iteritems(self._fields):
123 if field.has_value(self):
124 value = getattr(self, name)
125 res[name] = field.to_dict(value)
126 return res
37def dump(self, model):
38 """
39 Create a serialized dict from a Declarative model
40
41 :param DeclarativeMeta model: the model to be serialized
42
43 :rtype: dict
44 """
45 serial = {}
46 for attr, field in self._fields.items():
47 if field.load_only:
48 continue
49 value = getattr(model, attr) if hasattr(model, attr) else None
50 if field:
51 serialized = field.dump(value)
52 else:
53 serialized = value
54 serial[attr] = serialized
55 return serial
264def to_dict(self, nesting_level=1, serialize_reverse_relationships=None):
265 if isinstance(serialize_reverse_relationships, bool):
266 self.serialize_reverse_relationships = serialize_reverse_relationships
267 return self.recursive_to_dict(self.obj, nesting_level)

Related snippets