9π
The best solution to customise the queryset is using serializers.SerializerMethodField
, but what sheziβs reply is not exactly right. You need to return serializer.data
from SerializerMethodField
. So the solution should be like this:
class PhotoInAlbumSerializer(serialisers.ModelSerializer):
class Meta:
model = PhotoInAlbum
class AlbumSerializer(serializers.ModelSerializer):
# ...
photos = serializers.SerializerMethodField('get_photos_list')
def get_photos_list(self, instance):
photos = PhotoInAlbum.objects\
.filter(album_id=instance.id)\
.order_by('order')\
.values_list('photo_id', flat=True)
return PhotoInAlbumSerializer(photos, many=True, context=self.context).data
5π
It looks as if the RelatedManager that handles the relationship for ManyToManyFields does not respect ordering on the through model.
Since you cannot easily add an ordering parameter to the serializer field, the easiest way to achieve ordering is by using a serializer method:
class AlbumSerializer(serializers.modelSerializer):
# ...
photos = serializers.SerializerMethodField('get_photos_list')
def get_photos_list(self, instance):
return PhotoInAlbum.objects\
.filter(album_id=instance.id)\
.order_by('order')\
.values_list('photo_id', flat=True)
- Api key and Django Rest Framework Auth Token
- Docker + Django + Postgres Add-on + Heroku
- How to solve the ImportError: cannot import name simplejson in Django
2π
Generally, the easiest way is to do this in your AlbumView
or AlbumViewSet
.
You can do this by filtering β in this case you should define a get_queryset
method in your AlbumViewSet
.
Anyway, this is a good solution as long as you only GET the data. If you want to have POST and PUT methods working with ordering the photos, you can do it in two ways:
- stay with
ManyToMany
relation β patch thecreate
method inAlbumViewSet
and__create_items
andrestore_object
method inAlbumSerializer
or
- replace it with something more sophisticated β use django-sortedm2m field.
Note that the second solution does not mess with AlbumViewSet
(and even AlbumSerializer
!) β ordering logic stays in the relation field code.