3

我有这样的资源:

模型.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'

使用上述模型和资源,我想将 Place 列表视图中的评论数量限制为 3,详细/显示视图我想显示更多评论(可能的不同样式,例如,如果评论包含图像,则在详细视图中显示,隐藏它在列表视图中)

我试过放attribute=lambda bundle: Review.objects.all()[:3],,但每当我没有任何评论的地方它会失败并The model '' has an empty attribute ' at 0x7f0a180d0de8>' and doesn't allow a null value.显示消息。

对于这种情况,您有什么建议,这个问题有什么解决方法吗?

4

0 回答 0