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