Tadej, For multiple Walls yes there would be too many queries. I think I was only returning one Wall per request so it was ok for me.
While "ordering" on model meta class does solve the problem, it has the side affect of adding "order by" to all queries for that model class. It can add a lot of overhead for large querysets especially if the column isn't indexed or the index can't be used due to the query. Worst case is a lot of db work, creation of temp tables, etc. This can show up in weird places -- metrics runs, admin pages, etc. and cause issues. A solution at the serializer/view/queryset level would be more appropriate. I believe in newer versions of Django you can specify a custom queryset for a prefetch_related. That might be one way to go. Another option would be to add a custom property to the Wall model that just uses sorted() on the related queryset. Another benefit of those two approaches is that you can fully leverage prefetch_related(). Having a couple years more Django dev under my belt now, I think this is what I would do. -- sent from my phone -- > On Nov 6, 2016, at 17:38, Tadej Krevh <[email protected]> wrote: > > 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 a topic in the Google > Groups "Django REST framework" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/django-rest-framework/L9aXnwS4AQw/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > [email protected]. > For more options, visit https://groups.google.com/d/optout. -- 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.
