Hi Colin,
there is a very easy method of doing this. Just add ordering Meta
information to Brick class:
class Meta:
ordering = ['dateAdded']
Doing it via SerializerMethodField may not be the best solution, as you
will be firing additional queries to the database for each Wall.
Kind Regards,
Tadej
Dne sreda, 28. maj 2014 20.28.36 UTC+2 je oseba Colin Nichols napisala:
>
> Hi all,
>
> I have two models related to each other as follows:
>
> class Wall(models.Model):
> name = models.CharField(max_length=512)
> ....
>
> class Brick(models.Model):
> wall = models.ForeignKey(Wall, related_name='bricks')
> dateAdded = models.DateField()
>
> What I want is to create a view that, for a given Wall ID, returns the
> wall including list of associated Bricks, ordered by date added.
>
> Here's where I'm at... the View:
>
> class WallViewSet(viewsets.GenericViewSet,
> mixins.RetrieveModelMixin):
> model = models.Wall
> query_set = models.Wall.objects.all()
> serializer_class = serializers.WallSerializer
>
> And here are my serializers:
>
> class WallSerializer(serializers.ModelSerializer):
> bricks = BrickSerializer(many=True, read_only=True)
> class Meta:
> model = Wall
>
> class BrickSerializer(serializers.ModelSerializer):
> class Meta:
> model = Brick
>
> What is the best way to add sorting list of Bricks by date?
>
> Thanks in advance,
> Colin
>
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.