Skip to main content
update
Source Link

The answer from @zags is comprehensive and should suffice but the #5 method (which is the best one IMO) throws an error so I improved the helper function.

Edit:but the #5 method (which is the best one IMO) throws an error so I improved the helper function.

As the OP requested for converting many_to_many fields into a list of primary keys rather than a list of objects, I enhanced the function so the return value is now JSON serializable - by converting datetime objects into str and many_to_many objects to a list of id's.

import datetime def ModelToDict(instance): ''' Returns a dictionary object containing complete field-value pairs of the given instance Convertion rules: datetime.date --> str many_to_many --> list of id's ''' concrete_fields = instance._meta.concrete_fields m2m_fields = instance._meta.many_to_many data = {} for field in concrete_fields: key = field.name value = field.value_from_object(instance) if type(value) == datetime.datetime: value = str(field.value_from_object(instance)) data[key] = value for field in m2m_fields: key = field.name value = field.value_from_object(instance) data[key] = [rel.id for rel in value] return data 

The answer from @zags is comprehensive and should suffice but the #5 method (which is the best one IMO) throws an error so I improved the helper function.

Edit:

As the OP requested for converting many_to_many fields into a list of primary keys rather than a list of objects, I enhanced the function so the return value is now JSON serializable - by converting datetime objects into str and many_to_many objects to a list of id's.

import datetime def ModelToDict(instance): ''' Returns a dictionary object containing complete field-value pairs of the given instance Convertion rules: datetime.date --> str many_to_many --> list of id's ''' concrete_fields = instance._meta.concrete_fields m2m_fields = instance._meta.many_to_many data = {} for field in concrete_fields: key = field.name value = field.value_from_object(instance) if type(value) == datetime.datetime: value = str(field.value_from_object(instance)) data[key] = value for field in m2m_fields: key = field.name value = field.value_from_object(instance) data[key] = [rel.id for rel in value] return data 

The answer from @zags is comprehensive and should suffice but the #5 method (which is the best one IMO) throws an error so I improved the helper function.

As the OP requested for converting many_to_many fields into a list of primary keys rather than a list of objects, I enhanced the function so the return value is now JSON serializable - by converting datetime objects into str and many_to_many objects to a list of id's.

import datetime def ModelToDict(instance): ''' Returns a dictionary object containing complete field-value pairs of the given instance Convertion rules: datetime.date --> str many_to_many --> list of id's ''' concrete_fields = instance._meta.concrete_fields m2m_fields = instance._meta.many_to_many data = {} for field in concrete_fields: key = field.name value = field.value_from_object(instance) if type(value) == datetime.datetime: value = str(field.value_from_object(instance)) data[key] = value for field in m2m_fields: key = field.name value = field.value_from_object(instance) data[key] = [rel.id for rel in value] return data 
amend semantics
Source Link

The answer from @zags is comprehensive and should suffice but the #5 method (which is the best one IMO) throws an error so I improved the helper function. I avoided complicated code (aka pythonic!) in favor of readability:

Edit:

As the OP requested for converting many_to_many fields into a list of primary keys instead ofrather than a list of objects, I enhanced the function so itthe return value is now JSON serializable - by converting datetime objects into str and many_to_many objects to a list of id's.

import datetime def ModelToDict(instance): ''' Returns a dictionary object containing complete field-value pairs of the given instance Convertion rules: datetime.date --> str many_to_many --> list of id's ''' concrete_fields = instance._meta.concrete_fields m2m_fields = instance._meta.many_to_many data = {} for field in concrete_fields: key = field.name value = field.value_from_object(instance) if type(value) == datetime.datetime: value = str(field.value_from_object(instance)) data[key] = value for field in m2m_fields: key = field.name value = field.value_from_object(instance) data[key] = [rel.id for rel in value] return data 

The answer from @zags is comprehensive and should suffice but the #5 method (which is the best one IMO) throws an error so I improved the helper function. I avoided complicated code (aka pythonic!) in favor of readability:

Edit:

As the OP requested for converting many_to_many fields into a list of primary keys instead of list of objects, I enhanced the function so it is now JSON serializable - by converting datetime objects into str and many_to_many objects to a list of id's.

import datetime def ModelToDict(instance): ''' Returns a dictionary object containing complete field-value pairs of the given instance Convertion rules: datetime.date --> str many_to_many --> list of id's ''' concrete_fields = instance._meta.concrete_fields m2m_fields = instance._meta.many_to_many data = {} for field in concrete_fields: key = field.name value = field.value_from_object(instance) if type(value) == datetime.datetime: value = str(field.value_from_object(instance)) data[key] = value for field in m2m_fields: key = field.name value = field.value_from_object(instance) data[key] = [rel.id for rel in value] return data 

The answer from @zags is comprehensive and should suffice but the #5 method (which is the best one IMO) throws an error so I improved the helper function.

Edit:

As the OP requested for converting many_to_many fields into a list of primary keys rather than a list of objects, I enhanced the function so the return value is now JSON serializable - by converting datetime objects into str and many_to_many objects to a list of id's.

import datetime def ModelToDict(instance): ''' Returns a dictionary object containing complete field-value pairs of the given instance Convertion rules: datetime.date --> str many_to_many --> list of id's ''' concrete_fields = instance._meta.concrete_fields m2m_fields = instance._meta.many_to_many data = {} for field in concrete_fields: key = field.name value = field.value_from_object(instance) if type(value) == datetime.datetime: value = str(field.value_from_object(instance)) data[key] = value for field in m2m_fields: key = field.name value = field.value_from_object(instance) data[key] = [rel.id for rel in value] return data 
fixed typos
Source Link

The answer from @zags is comprehensive and should suffice but the #5 method (which is the best one IMO) throws an error so I improved the helper function. I avoided complicated code (aka pythonic!) in favor of readability:

Edit:

As the OP requested for converting many_to_many fields into a list of primary keys instead of list of objects, I enhanced the function so it is now JSON serializable - by converting datetime objects into str and many_to_many objects to a list of id's.

import datetime def ModelToDict(instance): ''' Returns a dictionary object containing complete field-value pairs of the given instance Convertion rules: datetime.date --> str many_to_many --> list of id's  ''' concrete_fields = instance._meta.concrete_fields m2m_fields = instance._meta.many_to_many data = {} for field in (concrete_fields:  + m2m_fields key = field.name value = field.value_from_object(instance) if type(value) == datetime.datetime:   value = str(field.value_from_object(instance)) data[key] = value for field in m2m_fields: key = field.name value = field.value_from_object(instance) data[key] = value[rel.id for rel in value] return data 

The answer from @zags is comprehensive and should suffice but the #5 method (which is the best one IMO) throws an error so I improved the helper function. I avoided complicated code (aka pythonic!) in favor of readability:

def ModelToDict(instance): ''' Returns a dictionary object containing complete field-value pairs of the given instance. ''' concrete_fields = instance._meta.concrete_fields m2m_fields = instance._meta.many_to_many data = {} for field in (concrete_fields + m2m_fields): key = field.name value = field.value_from_object(instance) data[key] = value return data 

The answer from @zags is comprehensive and should suffice but the #5 method (which is the best one IMO) throws an error so I improved the helper function. I avoided complicated code (aka pythonic!) in favor of readability:

Edit:

As the OP requested for converting many_to_many fields into a list of primary keys instead of list of objects, I enhanced the function so it is now JSON serializable - by converting datetime objects into str and many_to_many objects to a list of id's.

import datetime def ModelToDict(instance): ''' Returns a dictionary object containing complete field-value pairs of the given instance Convertion rules: datetime.date --> str many_to_many --> list of id's  ''' concrete_fields = instance._meta.concrete_fields m2m_fields = instance._meta.many_to_many data = {} for field in concrete_fields:   key = field.name value = field.value_from_object(instance) if type(value) == datetime.datetime:   value = str(field.value_from_object(instance)) data[key] = value for field in m2m_fields: key = field.name value = field.value_from_object(instance) data[key] = [rel.id for rel in value] return data 
code enhancement
Source Link
Loading
Source Link
Loading