Limit the number of ForeignKey items as a list

I have resources like:

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'

      

Using the above model and resource, I want to limit the number of views in the list view Place to 3, detail / show view. I want to show more reviews (maybe a different style, for example, if a review contains an image, show it in detail view, hide it as a list)

I tried to post attribute=lambda bundle: Review.objects.all()[:3],

, but whenever I have no reviews for a place it fails with a message The model '' has an empty attribute ' at 0x7f0a180d0de8>' and doesn't allow a null value.

.

What can you suggest for this case, are there any workarounds for this problem?

+3


source to share





All Articles