3π
β
I feel that this should be solved on the query level, ideally with prefetch_related
. But Django canβt slice Prefetch objects. Bummer. And Subquery is painstakingly slow.
The best solution I can think of, is creating a SerializerMethodField, which calls the TickerSerializer
with a sliced dataset. The drawback is that DRF will fire an additional SQL query for each Currency
object in the list.
from rest_framework.fields import SerializerMethodField
class CurrencySerializer(serializers.HyperlinkedModelSerializer):
ticker = SerializerMethodField()
def get_ticker(self, obj):
return TickerSerializer(
instance=obj.tickers.order_by('-created_at')[:1],
many=True
).data
class Meta:
model = Currency
fields = ('id', 'name','symbol', 'img', 'tickers', 'ticker')
π€masterfloda
1π
Think something like this would work, the problem with the current implementation is that the many=True
would get all the related tickers and return those objects in a list, which is the correct behaviour, but since you want to only display the latest ticker for each currency youβll need to serialize only the latest object.
class CurrencySerializer(serializers.HyperlinkedModelSerializer):
tickers = serializers.SerializerMethodField()
class Meta:
model = Currency
fields = ('id', 'name','symbol', 'img', 'tickers')
def get_tickers(self, obj):
data = {}
try:
latest_ticker = obj.tickers.latest('-created_at')
data = TickerSerializer(instance=latest_ticker).data
except Ticker.DoesNotExist:
pass
return data
π€Pieter Hamman
- [Django]-Django admin on different domain
- [Django]-Using TEMPLATE_STRING_IF_INVALID with Django 1.8
- [Django]-Overriding Django REST Frameworks update method to save the nested serializers
- [Django]-Add initial data to the database
- [Django]-Use case of try-except-else statement
Source:stackexchange.com