File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- # Copyright The IETF Trust 2015, All Rights Reserved
1+ # Copyright The IETF Trust 2015-2020, All Rights Reserved
2+
3+ import itertools
24
35from django .db import models
46
@@ -26,3 +28,17 @@ class OneToOneField(models.OneToOneField):
2628 def __init__ (self , to , on_delete = models .CASCADE , ** kwargs ):
2729 return super (OneToOneField , self ).__init__ (to , on_delete = on_delete , ** kwargs )
2830
31+ def object_to_dict (instance ):
32+ """
33+ Similar to django.forms.models.model_to_dict() but more comprehensive.
34+
35+ Taken from https://stackoverflow.com/questions/21925671/#answer-29088221
36+ with a minor tweak: .id --> .pk
37+ """
38+ opts = instance ._meta
39+ data = {}
40+ for f in itertools .chain (opts .concrete_fields , opts .private_fields ):
41+ data [f .name ] = f .value_from_object (instance )
42+ for f in opts .many_to_many :
43+ data [f .name ] = [i .pk for i in f .value_from_object (instance )]
44+ return data
You can’t perform that action at this time.
0 commit comments