【发布时间】:2016-11-12 08:39:20
【问题描述】:
我正在对 Product 模型及其 cmets 进行序列化。这是我的简单代码:
class ProductSerializer(serializers.HyperlinkedModelSerializer):
comment_set = CommentSerializer(many=True, read_only=True)
class Meta:
model = Product
fields = [
'title',
'comment_set'
]
class CommentSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Comment
fields = [
'text',
]
class Comment(models.Model):
product = models.ForeignKey(Product, null=True, blank=True, db_index=True)
class Product(models.Model):
title = models.CharField(max_length=50)
...
问题: 如果产品有很多 cmets。例如,500 厘米。全部 500 篇都被连载了。
如何将结果限制为我自己选择的数量,例如 100 cmets?
我在发布之前做了一些研究,但只发现了关于过滤的问题。
谢谢。
【问题讨论】:
标签: django serialization foreign-keys django-rest-framework foreign-key-relationship