在 Django Rest Framework 中序列化 ManyToMany 关系
Serializes ManyToMany relations in Django Rest Framework
我想序列化 ManyToMany 字段。因此,在 tje 应用程序模型的响应中还应该列出频段和频段描述。但是我收到以下错误消息:
Got AttributeError when attempting to get a value for field bands
on
serializer OsdSerializer
. The serializer field might be named
incorrectly and not match any attribute or key on the Application
instance. Original exception text was: 'Application' object has no
attribute 'bands'.
有人知道为什么它不起作用吗?
models.py
class Band(models.Model):
"""Database models for satellit's bands information"""
band = models.CharField(max_length=3)
description = models.CharField(max_length=255)
in_satellite = models.ForeignKey('Satellite', on_delete=models.PROTECT, blank=True, null=True)
wavelength = models.DecimalField(max_digits=4, decimal_places=0, default='0', help_text='Central wavelength (nm)')
resolution = models.DecimalField(max_digits=4, decimal_places=0, default='0', help_text='Spatial resolution (m)')
def __str__(self):
return '%s | %s' % (self.in_satellite, self.band)
class Satellite(models.Model):
"""Database models for satellite information"""
name = models.CharField(max_length=255)
accr = models.CharField(max_length=3)
operator = models.CharField(max_length=255, default='European Space Agency')
def __str__(self):
return self.name
class Indice(models.Model):
"""Database models for index information"""
name = models.CharField(max_length=255)
accr = models.CharField(max_length=10)
description = models.TextField(max_length=255, blank=True, null=True)
satellite_to_use = models.ForeignKey('Satellite', on_delete=models.PROTECT, blank=True, null=True)
needed_bands = models.ManyToManyField(Band)
def __str__(self):
return self.name
class Application(models.Model):
"""Database models for application information"""
name = models.CharField(max_length=255)
description = models.TextField(max_length=255, blank=True, null=True)
indice_to_use = models.ForeignKey('Indice', on_delete=models.PROTECT, blank=True, null=True)
def __str__(self):
return self.name
serializers.py
class BandSerializer(serializers.ModelSerializer):
class Meta:
model = Band
fields = ['band', 'description', ]
class OsdSerializer(serializers.ModelSerializer):
bands = BandSerializer()
class Meta:
model = Application
fields = ['name', 'description', 'bands',]
views.py
class OsdView(APIView):
def get(self, request):
applications = Application.objects.all()
serializer = OsdSerializer(applications, many=True)
return Response({"Your open space data:": serializer.data})
您的代码是正确的,但在这里我没有看到您的两个型号 Band 和 Application 之间有任何关系。
我想你可能需要找到 Band 模型和 Application 模型之间的一些关系,然后你会在输出中获得与相应应用程序对象关联的 band 对象。
IIUC,您需要更改 OsdSerializer
以指定要包含在序列化数据中的关系:
bands = BandSerializer(<b>source='indice_to_use.needed_bands', many=True</b>)
我想序列化 ManyToMany 字段。因此,在 tje 应用程序模型的响应中还应该列出频段和频段描述。但是我收到以下错误消息:
Got AttributeError when attempting to get a value for field
bands
on serializerOsdSerializer
. The serializer field might be named incorrectly and not match any attribute or key on theApplication
instance. Original exception text was: 'Application' object has no attribute 'bands'.
有人知道为什么它不起作用吗?
models.py
class Band(models.Model):
"""Database models for satellit's bands information"""
band = models.CharField(max_length=3)
description = models.CharField(max_length=255)
in_satellite = models.ForeignKey('Satellite', on_delete=models.PROTECT, blank=True, null=True)
wavelength = models.DecimalField(max_digits=4, decimal_places=0, default='0', help_text='Central wavelength (nm)')
resolution = models.DecimalField(max_digits=4, decimal_places=0, default='0', help_text='Spatial resolution (m)')
def __str__(self):
return '%s | %s' % (self.in_satellite, self.band)
class Satellite(models.Model):
"""Database models for satellite information"""
name = models.CharField(max_length=255)
accr = models.CharField(max_length=3)
operator = models.CharField(max_length=255, default='European Space Agency')
def __str__(self):
return self.name
class Indice(models.Model):
"""Database models for index information"""
name = models.CharField(max_length=255)
accr = models.CharField(max_length=10)
description = models.TextField(max_length=255, blank=True, null=True)
satellite_to_use = models.ForeignKey('Satellite', on_delete=models.PROTECT, blank=True, null=True)
needed_bands = models.ManyToManyField(Band)
def __str__(self):
return self.name
class Application(models.Model):
"""Database models for application information"""
name = models.CharField(max_length=255)
description = models.TextField(max_length=255, blank=True, null=True)
indice_to_use = models.ForeignKey('Indice', on_delete=models.PROTECT, blank=True, null=True)
def __str__(self):
return self.name
serializers.py
class BandSerializer(serializers.ModelSerializer):
class Meta:
model = Band
fields = ['band', 'description', ]
class OsdSerializer(serializers.ModelSerializer):
bands = BandSerializer()
class Meta:
model = Application
fields = ['name', 'description', 'bands',]
views.py
class OsdView(APIView):
def get(self, request):
applications = Application.objects.all()
serializer = OsdSerializer(applications, many=True)
return Response({"Your open space data:": serializer.data})
您的代码是正确的,但在这里我没有看到您的两个型号 Band 和 Application 之间有任何关系。
我想你可能需要找到 Band 模型和 Application 模型之间的一些关系,然后你会在输出中获得与相应应用程序对象关联的 band 对象。
IIUC,您需要更改 OsdSerializer
以指定要包含在序列化数据中的关系:
bands = BandSerializer(<b>source='indice_to_use.needed_bands', many=True</b>)