1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | def instance_dict(instance, key_format=None):
"Returns a dictionary containing field names and values for the given instance"
from django.db.models.fields.related import ForeignKey
if key_format:
assert '%s' in key_format, 'key_format must contain a %s'
key = lambda key: key_format and key_format % key or key
d = {}
for field in instance._meta.fields:
attr = field.name
value = getattr(instance, attr)
if value is not None and isinstance(field, ForeignKey):
value = value._get_pk_val()
d[key(attr)] = value
for field in instance._meta.many_to_many:
d[key(field.name)] = [obj._get_pk_val() for obj in getattr(instance, field.attname).all()]
return d
|
Comments
Ticket #5126 has a patch for including this functionality in Django.
#
The function didn't handle dates correctly, at least when feeding instance data to a form with a SelectDateWidget.
I also wanted to use it for unsaved objects, but they failed on many-to-many fields.
Here's a version which fixes both these problems:
#