缺少带有ManyToManyField的序列化模型的数据



以下是我的例子:

型号.py:

class Example(models.Model):
title = models.CharField(...)
description = models.CharField(...)
class Foo(models.Model):
example = models.ManyToManyField(Example)

序列化程序.py:

class FooSerializer(serializers.ModelSerializer):
class Meta:
model = Foo
fields = '__all__'
depth = 1

视图.py:

...
serialized_data = [FooSerializer(foo).data for foo in Foo.objects.all().get]

在输出中,我只收到Example的ID,但有没有任何方法可以同时获得标题和描述字段(m2mfield的详细信息(?据我所知,Foo.objects.all().get根本不包含这些数据,但也许我可以以某种方式获得并使用它?如果需要,我也可以重建模型,但目前我使用m2mf,因为需要包含与此模型数据相关的多个对象。

更新

型号.py

class Event(models.Model):
ts = models.BigIntegerField(editable=False)
class Foo(Event):
user = models.ForeignKey(User, ...)
example = *...(remains to be the same)*
foos = models.ForeignKey('self', **somemore** null=True)

序列化程序.py

class EventSerializer(serializers.ModelSerializer):
class Meta:
model = Event
fields = '__all__'
def to_representation(self, instance):
result = {'ts': instance.ts}
if isinstance(instance, Foo):
result['foo'] = FooSerializer(instance).data
return result
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'username')
class FooSerializer(serializers.ModelSerializer):
# user = UserSerializer(read_only=True) # with this I have an error: Got AttributeError when attempting to get a value for field 'username' on #serializer 'UserSerializer'
class Meta:
model = Foo
fields = '__all__'
depth = 1

您可以使用depth属性来实现所需的输出。

默认的ModelSerializer为关系使用主键,但是也可以使用depth选项轻松生成嵌套表示。depth选项应设置为表示之前应该遍历的关系的深度恢复为平面表示

class FooSerializer(serializers.ModelSerializer):
class Meta:
model = Foo
fields = '__all__'
depth = 1



除了答案之外,我想更改您的views.py代码,因为它看起来很糟糕:(.在DRF方式上执行,作为

serialized_data = FooSerializer(Foo.objects.all(), many=True).data<br>

示例视图

from rest_framework.viewsets import ModelViewSet

class FooViewset(ModelViewSet):
serializer_class = FooSerializer
queryset = Foo.objects.all()


更新-1

class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
exclude = ('password',) # add fields that are need to be excluded

class FooSerializer(serializers.ModelSerializer):
user = UserSerializer()
class Meta:
model = Foo
fields = '__all__'
depth = 1

depth = 1将序列化model中的所有字段,(与在序列化程序的Meta类中设置fields=='__all__'相同(


UPDATE-2

class FooSerializer(serializers.ModelSerializer):
user = UserSerializer()
class Meta:
model = Foo
fields = '__all__'
depth = 1
def to_representation(self, instance):
real_data = super().to_representation(instance).copy()
# DO YOUR EXTRA CHECKS
child = UserSerializer(instance.child_foo).data
if child:
real_data.update({"child_data": child})
# After your checks, add it to "real_data"
return real_data

我假设我有一个Foo模型作为

class Foo(models.Model):
example = models.ManyToManyField(Example)
user = models.ForeignKey(User)
child_foo = models.ForeignKey('self', null=True, blank=True)

在序列化程序中添加depth=1。"用户"是相关字段的示例:

FooSerializer(serializers.ModelSerializer):
class Meta:
model = Foo
fields = ('id', 'account_name', 'users', 'created')
depth = 1

相关内容

  • 没有找到相关文章

最新更新