Django-Rest-Framework: Endpoint Using a Related Field ID in the URL

Viewed 22

I have a MarketHistoryListing model that looks like the following:

class MarketHistoryListing(auto_prefetch.Model):
    player_profile = auto_prefetch.ForeignKey(PlayerProfile, on_delete=models.SET_NULL, null=True)
    sales_minute = models.DecimalField(
        max_digits=1000, decimal_places=2, null=True)

I'd like to setup an endpoint where I can get all of the MarketHistoryListing objects that below to a specific player_profile. Something like /api/market-history/<player_profile._id>

I setup what I think is the correct way to do the view - but i'm not 100% sure.

class MarketHistoryListingView(viewsets.ModelViewSet):
    serializer_class = MarketHistoryListingSerializer
    queryset = MarketHistoryListing.objects.all()
    pagination_class = LargeResultsSetPagination

def get_queryset(self):
    return MarketHistoryListing.objects.filter(player_profile=self.kwargs['user_inventory_pk'])

However, I really don't know how to do the URL. I'm assuming it will be something similar tot he following, but i'm not sure how to pass int he dynamic player profile ID.

market_history_router = routers.NestedSimpleRouter(
    router,
    r'market-hitory',
    lookup='user_inventory'
)

market_history_router.register(
    <player_id>,
    views.MarketHistoryListingView,
    basename=<player_id>
)

Appreciate any help!

0 Answers
Related