Hi Colin,
I a have a similar problem related to ordering. Problem is applying
multiple order by in foreign key fields,
Sample code snippet:
class Game(models.Model):
player = models.ForeignKey(place, null=True, blank=True,
verbose_name="place")
ground = models.ForeignKey(country, null=True, blank=True,
verbose_name="place")
total_score = models.ForeignKey(score, null=True, blank=True,
verbose_name="place")
class Place(Time):
child = models.CharField(max_length=100, blank=True,
verbose_name="child", db_index=True)
class country(Time):
child = models.CharField(max_length=100, blank=True,
verbose_name="country", db_index=True)
class Time (models.Model):
time = models.CharField(max_length=100, null=True, blank=True,
verbose_name="time")
Trying to sort based on time:
Game.objects.filter(...).order_by('-player__time','ground__time')
Winded up with no success.Can you please the problem as you already worked
on the similar issue.
On Wednesday, June 4, 2014 at 3:37:08 AM UTC+5:30, Colin Nichols wrote:
>
> OK, got it while working on a related problem. Here is how I did it:
>
> class WallSerializer(serializers.ModelSerializer):
> class Meta:
> model = Wall
>
> bricks = serializers.SerializerMethodField('get_bricks')
>
> def get_bricks(selfself, obj):
> qset = Brick.objects.filter(brick__pk=obj.pk
> ).order_by('-date_added')
> ser = BrickSerializer(qset, many=True, read_only=True)
> return ser.data
>
>
> On Wednesday, May 28, 2014 4:01:01 PM UTC-4, Colin Nichols wrote:
>>
>> Thanks Xavier! I agree... but I've already spent too much time on
>> this... popping the chute and using sorted() :)
>>
>> Still interested in figuring this out, if anyone has other insights...
>> just spent too much time on it for now.
>>
>> class WallViewSet(viewsets.ViewSet):
>> model = models.Wall
>>
>> def retrieve(self, request, pk=None):
>> queryset = models.Wall.objects.all()
>> wall = get_object_or_404(queryset, pk=pk)
>> serializer = serializers.WallSerializer(wall)
>> data = serializer.data
>> data['bricks'] = sorted(data['bricks'],key=lambda brik:
>> brik["date_added"], reverse=True)
>> return Response(data)
>>
>>
>> Thanks,
>> Colin
>>
>> On Wednesday, May 28, 2014 3:43:07 PM UTC-4, Xavier Ordoquy wrote:
>>>
>>> Hi Colin,
>>>
>>> This should be possible using Django's order_by although I must confess
>>> I have never used it on related queries.
>>>
>>> Regards,
>>> Xavier,
>>> Linovia.
>>>
>>> Le 28 mai 2014 à 21:36, Colin Nichols <[email protected]> a écrit :
>>>
>>> Hi Xavier,
>>>
>>> Thanks for the reply. Indeed it is related but I think the solution to
>>> my problem lies elsewhere. The above link is concerned with ordering the
>>> overall result set by a subfield; whereas I would like to specify the
>>> ordering of an array of subfields on a single result. I took a good look
>>> thru the code and tried it out, but I'm not sure how I'd adapt it to my
>>> particular issue, because I don't know how to get at the queryset for the
>>> list of related objects.
>>>
>>> Is it possible to specify the ordering of the related objects (bricks in
>>> my example above, or tracks in the Album/Track example in documentation)?
>>>
>>> Thanks again,
>>> Colin
>>>
>>> On Wednesday, May 28, 2014 2:32:46 PM UTC-4, Xavier Ordoquy wrote:
>>>>
>>>> Hi Colin,
>>>>
>>>> You might be interested in
>>>> https://github.com/tomchristie/django-rest-framework/issues/1005
>>>> It looks similar to your current issue.
>>>>
>>>> Regards,
>>>> Xavier,
>>>> Linovia.
>>>>
>>>> Le 28 mai 2014 à 20:28, Colin Nichols a écrit :
>>>>
>>>> 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.
>>>>
>>>>
>>>>
>>> --
>>> 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.
>>>
>>>
>>>
--
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.