Python 在Django Rest框架中获取相关模型的值?

Python 在Django Rest框架中获取相关模型的值?,python,django,django-rest-framework,Python,Django,Django Rest Framework,我想以序列化的形式获取产品的所有图像。我的模型如下 class Product(): title subtitle ... class ProductImage(): product = models.ForeignKey( 'Product', related_name='images', verbose_name=_("Product")) image_path 我的序列化程序: class ProductImageSerializer(

我想以序列化的形式获取产品的所有图像。我的模型如下

class Product():
    title 
    subtitle 
    ...
class ProductImage():
    product = models.ForeignKey(
    'Product', related_name='images', verbose_name=_("Product"))
    image_path
我的序列化程序:

class ProductImageSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = ProductImage
        fields = ('caption', 'display_order', 'original', 'product')


class ProductSerializer(serializers.HyperlinkedModelSerializer):

    images = ProductImageSerializer()
    class Meta:
        model = Product
        fields = (
            'title', 'slug', 'short_description', 'description',
            'sku', 'pk', 'images')
我得到了这个错误

AttributeError at/api/products/ 尝试获取序列化程序“ProductImageSerializer”上字段“display\u order”的值时获取AttributeError。 序列化程序字段的名称可能不正确,并且与“RelatedManager”实例上的任何属性或键都不匹配。 原始异常文本为:“RelatedManager”对象没有“显示顺序”属性


如何获取特定产品的所有图像?

您应该定义相关模型实例的源,并设置
many=True

images = ProductImageSerializer(many=True, source='images.all')

仍然获取属性错误:我正在使用
相关的\u名称(图像)
进行查询(请参阅productimage模型,我更改了它)。我的查询:
images=ProductImageSerializer(many=True,source='product\u images.all')
现在得到:
字段选项必须是列表或元组或“\u all\u”。得到str.
关键是你必须定义一个源。我可能对语法有错误,也许应该是
source='images'
。好的。在shell中,如果我执行
product.images.all()
操作,我将获得一个包含所有图像对象的查询集。这是一个序列化图像对象的问题吗?一点也不,你只需要找到正确的语法就可以了。官方教程的这一部分可能有助于: