Skip to content

There is a way to limit nested objects from HyperlinkedRelatedField ? #7738

@MkLHX

Description

@MkLHX

Checklist

  • I have verified that that issue exists against the master branch of Django REST framework.
  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • This is not a usage question. (Those should be directed to the discussion group instead.)
  • This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • I have reduced the issue to the simplest possible case.
  • I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Hi, i'm looking for answer about limiting nested objects return from HyperlinkedRelatedField.

I don't speak about django pagination for a View or DRF pagination for Viewset.
My Models:

class Pump(models.Model):
    def __unicode__(self):
        if self.objects.count() > 5:
            raise Exception("cannot insert more than 5 pumps for one waterpump")

    water_pump = models.ForeignKey(
        WaterPump, on_delete=models.CASCADE, related_name="pumps"
    )

    def __str__(self):
        return "pump-%s-%s" % (self.id, self.water_pump.unique_id)

    class Meta:
        pass


class PumpSetting(models.Model):
    pump_type = models.IntegerField(default=0)
    pump_duration = models.IntegerField(default=0)
    pump_state = models.BooleanField(default=False)
    at = models.DateTimeField(auto_now_add=True, blank=True)
    pump = models.ForeignKey(
        Pump, on_delete=models.CASCADE, related_name="pump_settings"
    )

    class Meta:
        pass

My ViewSets:

class PumpViewSet(MyReadOnlyViewset):
    """@brief
    API endpoint that allows hub's slaves waterpumps pumps to be viewed or edited.
    """

    queryset = core_models.Pump.objects.all().order_by("id")
    serializer_class = serializers.PumpSerializer
    filter_backends = [DjangoFilterBackend]
    filterset_fields = [
        "id",
        "water_pump",
        "water_pump__unique_id",
    ]
    pagination_class = LimitOffsetPagination
    permission_classes = [permissions.AllowAny]
    
class PumpSettingViewSet(MyReadOnlyViewset):
    """@brief
    API endpoint that allows hub's slaves waterpumps pump settings to be viewed or edited.
    """

    throttle_classes = [UserRateThrottle]
    queryset = core_models.PumpSetting.objects.all().order_by("-id")

    serializer_class = serializers.PumpSettingSerializer
    filter_backends = [DjangoFilterBackend]
    filterset_fields = [
        "id",
        "pump_type",
        "pump_state",
        "at",
        "pump",
    ]
    pagination_class = LimitOffsetPagination
    permission_classes = [permissions.AllowAny]

My Serializers:

class PumpSerializer(serializers.HyperlinkedModelSerializer):
    pump_settings = serializers.HyperlinkedRelatedField(
        view_name="pumpsetting-detail", many=True, read_only=True
    )
    class Meta:
        model = core_models.Pump
        fields = [
            "id",
            "resource_uri",
            "pump_settings",
            "water_pump",
        ]
        depth = 1
        read_only_fields = [
            "id",
            "pump_settings",
            "water_pump",
        ]

class PumpSettingSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = core_models.PumpSetting
        fields = [
            "id",
            "resource_uri",
            "pump_type",
            "pump_duration",
            "pump_state",
            "at",
            "pump",
        ]
        depth = 2
        read_only_fields = ["id", "pump", "at"]

the api result (too large nested objects):

HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 1,
    "resource_uri": "https://hub-e61.local/api/v2/pumps/1/",
    "pump_settings": [
        "https://hub-e61.local/api/v2/pumpsettings/1/",
        "https://hub-e61.local/api/v2/pumpsettings/265/",
        "https://hub-e61.local/api/v2/pumpsettings/266/",
        "https://hub-e61.local/api/v2/pumpsettings/910/",
        "https://hub-e61.local/api/v2/pumpsettings/912/",
        "https://hub-e61.local/api/v2/pumpsettings/1781/",
        "https://hub-e61.local/api/v2/pumpsettings/1814/",
        "https://hub-e61.local/api/v2/pumpsettings/1988/",
        "https://hub-e61.local/api/v2/pumpsettings/2155/",
        "https://hub-e61.local/api/v2/pumpsettings/2217/",
        "https://hub-e61.local/api/v2/pumpsettings/2841/",
        "https://hub-e61.local/api/v2/pumpsettings/3006/",
        "https://hub-e61.local/api/v2/pumpsettings/3246/",
        "https://hub-e61.local/api/v2/pumpsettings/3342/",
        "https://hub-e61.local/api/v2/pumpsettings/6730/",
        "https://hub-e61.local/api/v2/pumpsettings/6741/",
        /../ too many more records
      ],
      "water_pump": {
        "resource_uri": "https://hub-e61.local/api/v2/waterpumps/1/",
        "unique_id": "227181158168",
        "firmware": "10",
        "ip_address": "0.0.0.0",
        "mdns": "water-pump-a60.local",
        "hub_unique_id": "hub-e61",
        "pair_status": 127,
        "i2c_device_type": 1,
        "i2c_address": 32,
        "leds_status": 1
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions