简体   繁体   中英

Django - serialize model objects with dependencies

In serializing a queryset of TestChild model objects, I'd like the json to include dependent TestParent objects.

Here's my code:

from django.db import models


class TestParentManager(models.Manager):
    def get_by_natural_key(self, field1, field2):
        return self.get(field1=field1, field2=field2)


class TestParent(models.Model):
    field1 = models.CharField(max_length=20, blank=True, default='')
    field2 = models.CharField(max_length=20, blank=True, default='')
    field3 = models.CharField(max_length=20, blank=True, default='')

    objects = TestParentManager()

    def natural_key(self):
        return (self.field1, self.field2)

    class Meta:
        unique_together = [['field1', 'field2']]


class TestChildManager(models.Manager):
    def get_by_natural_key(self, field1, field2):
        return self.get(field1=field1, field2=field2)


class TestChild(models.Model):
    parent = models.ForeignKey(TestParent, on_delete=models.CASCADE)
    field1 = models.CharField(max_length=20, blank=True, default='')
    field2 = models.CharField(max_length=20, blank=True, default='')
    field3 = models.CharField(max_length=20, blank=True, default='')

    objects = TestChildManager()

    def natural_key(self):
        return (self.field1, self.field2)
    natural_key.dependencies = ['environ_tool.testparent']

    class Meta:
        unique_together = [['field1', 'field2']]

When I run

serializers.serialize(
    'json',
    eModels.TestChild.objects.all(),
    use_natural_foreign_keys=True,
    use_natural_primary_keys=True,
)

the resulting json is like so (no TestParent objects):

[{u'fields': {u'field1': u'f1',
              u'field2': u'f2',
              u'field3': u'f3',
              u'parent': [u'foo2', u'']},
  u'model': u'environ_tool.testchild'}]

but I would like to see the TestParent object that the TestChild object depends on in the json too, like so:

[{u'fields': {u'field1': u'foo2', u'field2': u'', u'field3': u''},
  u'model': u'environ_tool.testparent'},
 {u'fields': {u'field1': u'f1',
              u'field2': u'f2',
              u'field3': u'f3',
              u'parent': [u'foo2', u'']},
  u'model': u'environ_tool.testchild'}]

The django docs lead me to believe this is supposed to happen in serialization but no luck. I'm using Django 1.11 and here's the doc page https://docs.djangoproject.com/en/1.11/topics/serialization/

Thank you!

You can write serializer class for child. You will have to use django-rest-framework ModelSerializer.

class ChildSerializer(serializers.ModelSerializer):
    class Meta:
        model = childmodel
        fields = '__all__'
        depth = 1

and you can call it like

serializer = ChildSerializer([childinstance1, ], many=True)

then you can convert it to json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM