我有这样的资源:
models.py
class Place(models.Model):
id = models.CharField(max_length = 256, primary_key = True)
name = models.CharField(max_length = 1024)
class Review(models.Model):
id = models.CharField(max_length = 256, primary_key = True)
p_id = models.ForeignKey(Place, related_name = 'place_review')
text = models.TextField()
api.py
class ReviewResource(ModelResource):
class Meta:
queryset = Review.objects.all()
resource_name = 'place_review'
class PlaceResource(ModelResource):
place_review = fields.OneToManyField(ReviewResource,
'place_review',
full=True)
class Meta:
queryset = Place.objects.all()
resource_name = 'place'
使用上面的模型和资源,我想限制地点到3的列表视图中的评论数量,详细/显示视图我想要显示更多评论(可能的不同风格,例如,如果评论包含图像,请详细显示查看,将其隐藏在列表视图中)
我试图提出attribute=lambda bundle: Review.objects.all()[:3],
,但每当我对地方没有任何评论时,它就会失败并显示The model '' has an empty attribute ' at 0x7f0a180d0de8>' and doesn't allow a null value.
消息。
对于这种情况,您能提出什么建议,是否有解决此问题的方法?