Django REST nested annotate fields in serializers

2,657 views
Skip to first unread message

Robin Vanderveken

unread,
Oct 15, 2015, 5:04:36 PM10/15/15
to Django REST framework
Hi

While implementing the Django REST framework in the Django environment, I encountered a problem with annotated fields. In Django you can simply use the annotate field to make per object summaries and it works perfectly. This also works nested.

However while parsing a nested annotated queryset in a nested Django REST serializer, the serializer doesn't recognize the field. I posted this problem with an example on Stack overflow: http://stackoverflow.com/questions/32964820/nested-annotate-fields-in-django-rest-framework-serializers
 .

I discussed this with my colleagues and I don't seem to be doing any bad practices. Is this a feature that is not implemented in the Django REST framework? Or are there any other possible solutions for this? With the regular SerializerMethodField I can easily 1000+ queries is my use-case, which is not very optimal.

Kind regards, Robin

Tom Christie

unread,
Oct 20, 2015, 5:58:47 AM10/20/15
to Django REST framework
The bit that jumps out to me is that annotated fields should be read_only, right? (Which isn't the case in your example on S/O) Perhaps that's the problem you're running into.

Failing that, perhaps you could be more specific about what you mean here?...

Robin Vanderveken

unread,
Oct 23, 2015, 4:26:08 AM10/23/15
to django-res...@googlegroups.com
Thank you for your reply, using read_only does indeed remove the error, but it doesn't show the field anymore. I was trying to show a field per ice cream truck that contains the amount of drivers linked to that truck. And that per truck.

I think I'm overestimating the powers of Django here as my queries didn't end up looking like what I needed. I ended up needing more details anyways then a simple counter so a custom view is better for my situation, thanks again.

--
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 django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Disclaimer

Les informations contenues dans ce message sont destinées exclusivement à l’usage de la personne à laquelle elles sont adressées. Elles peuvent contenir des données confidentielles et/ou privilégiées et/ou protégées par des droits de propriété intellectuelle ou d’autres lois. Si vous avez reçu ce message par erreur, veuillez le détruire, ainsi que toutes ses annexes, et notifier son expéditeur ; il vous est interdit de copier ou d’utiliser ce messages ou ses annexes ou de divulguer son contenu à quiconque. La transmission de données par e-mail ne peut être garantie comme un moyen sûr et infaillible, ni comme étant exempt de tout virus. L’expéditeur décline toute responsabilité en cas de perte ou de dommage résultant de ce message ou de son utilisation, quelle qu’elle soit. 

The information contained in this message is intended for the addressee only and may contain confidential and/or privileged information and/or information protected by intellectual property rights or other legal rules. If you are not the intended recipient, please delete this message and any attachment to it and notify the sender; you may not copy or use this message or its attachments in any way nor disclose its contents to anyone. Emails cannot be guaranteed to be secure or to be error or virus free. No liability is accepted by the sender for any loss damage arising in any way from this message or its use. 
Reply all
Reply to author
Forward
0 new messages