Skip to main content
added 1 character in body
Source Link
Zags
  • 41.9k
  • 16
  • 125
  • 157

Django Rest Framework's ModelSerialzerModelSerializer allows you to build a serializer automatically from a model.

Django Rest Framework's ModelSerialzer allows you to build a serializer automatically from a model.

Django Rest Framework's ModelSerializer allows you to build a serializer automatically from a model.

added 14 characters in body
Source Link
Zags
  • 41.9k
  • 16
  • 125
  • 157
from itertools import chain def to_dict(instance): opts = instance._meta data = {} for f in chain(opts.concrete_fields, opts.private_fields): data[f.name] = f.value_from_object(instance) for f in opts.many_to_many: data[f.name] = [i.id for i in f.value_from_object(uinstance)] return data 
from django.db import models from itertools import chain class PrintableModel(models.Model): def __repr__(self): return str(self.to_dict()) def to_dict(instance): opts = instance._meta data = {} for f in chain(opts.concrete_fields, opts.private_fields): data[f.name] = f.value_from_object(instance) for f in opts.many_to_many: data[f.name] = [i.id for i in f.value_from_object(uinstance)] return data class Meta: abstract = True 
from itertools import chain def to_dict(instance): opts = instance._meta data = {} for f in chain(opts.concrete_fields, opts.private_fields): data[f.name] = f.value_from_object(instance) for f in opts.many_to_many: data[f.name] = [i.id for i in f.value_from_object(u)] return data 
from django.db import models from itertools import chain class PrintableModel(models.Model): def __repr__(self): return str(self.to_dict()) def to_dict(instance): opts = instance._meta data = {} for f in chain(opts.concrete_fields, opts.private_fields): data[f.name] = f.value_from_object(instance) for f in opts.many_to_many: data[f.name] = [i.id for i in f.value_from_object(u)] return data class Meta: abstract = True 
from itertools import chain def to_dict(instance): opts = instance._meta data = {} for f in chain(opts.concrete_fields, opts.private_fields): data[f.name] = f.value_from_object(instance) for f in opts.many_to_many: data[f.name] = [i.id for i in f.value_from_object(instance)] return data 
from django.db import models from itertools import chain class PrintableModel(models.Model): def __repr__(self): return str(self.to_dict()) def to_dict(instance): opts = instance._meta data = {} for f in chain(opts.concrete_fields, opts.private_fields): data[f.name] = f.value_from_object(instance) for f in opts.many_to_many: data[f.name] = [i.id for i in f.value_from_object(instance)] return data class Meta: abstract = True 
deleted 394 characters in body
Source Link
Zags
  • 41.9k
  • 16
  • 125
  • 157

The code for django's model_to_dict had most of the answer. It explicitly removed non-editable fields, so removing that check and getting the ids of foreign keys for many to many fields results in the following code which behaves as desired:

from django.db.models.fields.relateditertools import ManyToManyFieldchain def to_dict(instance): opts = instance._meta data = {} for f in chain(opts.concrete_fields +, opts.many_to_many: if isinstance(f, ManyToManyFieldprivate_fields): if instance.pk is None: data[f.name] = [] else:   data[f.name] = list(f.value_from_object(instance).values_list('pk', flat=True))   for f in elseopts.many_to_many:   data[f.name] = [i.id for i in f.value_from_object(instanceu)] return data 
from django.db import models from django.db.models.fields.relateditertools import ManyToManyFieldchain class PrintableModel(models.Model): def __repr__(self): return str(self.to_dict())   def to_dict(selfinstance): opts = selfinstance._meta data = {} for f in chain(opts.concrete_fields +, opts.many_to_many: if isinstance(f, ManyToManyFieldprivate_fields): if self.pk is None: data[f.name] = [] else:   data[f.name] = list(f.value_from_object(self).values_list('pk', flat=True)instance)   for f in elseopts.many_to_many:   data[f.name] = [i.id for i in f.value_from_object(selfu)] return data class Meta: abstract = True 

The code for django's model_to_dict had most of the answer. It explicitly removed non-editable fields, so removing that check results in the following code which behaves as desired:

from django.db.models.fields.related import ManyToManyField def to_dict(instance): opts = instance._meta data = {} for f in opts.concrete_fields + opts.many_to_many: if isinstance(f, ManyToManyField): if instance.pk is None: data[f.name] = [] else:   data[f.name] = list(f.value_from_object(instance).values_list('pk', flat=True))   else:   data[f.name] = f.value_from_object(instance) return data 
from django.db import models from django.db.models.fields.related import ManyToManyField class PrintableModel(models.Model): def __repr__(self): return str(self.to_dict())   def to_dict(self): opts = self._meta data = {} for f in opts.concrete_fields + opts.many_to_many: if isinstance(f, ManyToManyField): if self.pk is None: data[f.name] = [] else:   data[f.name] = list(f.value_from_object(self).values_list('pk', flat=True))   else:   data[f.name] = f.value_from_object(self) return data class Meta: abstract = True 

The code for django's model_to_dict had most of the answer. It explicitly removed non-editable fields, so removing that check and getting the ids of foreign keys for many to many fields results in the following code which behaves as desired:

from itertools import chain def to_dict(instance): opts = instance._meta data = {} for f in chain(opts.concrete_fields, opts.private_fields): data[f.name] = f.value_from_object(instance) for f in opts.many_to_many: data[f.name] = [i.id for i in f.value_from_object(u)] return data 
from django.db import models from itertools import chain class PrintableModel(models.Model): def __repr__(self): return str(self.to_dict()) def to_dict(instance): opts = instance._meta data = {} for f in chain(opts.concrete_fields, opts.private_fields): data[f.name] = f.value_from_object(instance) for f in opts.many_to_many: data[f.name] = [i.id for i in f.value_from_object(u)] return data class Meta: abstract = True 
added 3 characters in body
Source Link
Zags
  • 41.9k
  • 16
  • 125
  • 157
Loading
added 3 characters in body
Source Link
Zags
  • 41.9k
  • 16
  • 125
  • 157
Loading
added 3 characters in body
Source Link
Zags
  • 41.9k
  • 16
  • 125
  • 157
Loading
deleted 1 character in body
Source Link
Zags
  • 41.9k
  • 16
  • 125
  • 157
Loading
more code markup, minor clarifications
Source Link
Lutz Prechelt
  • 40.1k
  • 11
  • 71
  • 90
Loading
Moving answer portion of question to answer
Source Link
Zags
  • 41.9k
  • 16
  • 125
  • 157
Loading
Source Link
Zags
  • 41.9k
  • 16
  • 125
  • 157
Loading