50👍
Why using many=True. Parent is just a single field, no need to use explicit serializer field. Just get rid of these many=True
-answered by mariodev in comment.
0👍
You can do something like this using python collections as an intermediate
#serializers.py
class TimelineSerializer(serializers.Serializer):
childs= childSerializer(many=True)
parents = parentSerializer(many=True)
#apiviews.py
from collections import namedtuple
Timeline = namedtuple('Timeline', ('childs', 'parents'))
def list(self, request):
timeline = Timeline(
childs=Child.objects.all(),
parents=Parent.objects.all(),
)
serializer = TimelineSerializer(timeline)
return Response(serializer.data)
- [Django]-Simple way to reset Django PostgreSQL database?
- [Django]-How do I rename a Django project in PyCharm?
- [Django]-Django celery task: Newly created model DoesNotExist
0👍
If your using a ModalSerializer then you have two foreign keys in a modal, then use "to_representation" function like this
def to_representation(self, instance):
rep = super().to_representation(instance)
rep['pcatalog_name'] = CatalogSerializer(instance.pcatalog_name).data
rep['pcategory_name'] = CategorySerializer(instance.pcategory_name).data
return rep
- [Django]-AttributeError while using Django Rest Framework with serializers
- [Django]-Nullable ForeignKey fields in Django REST framework
- [Django]-How to change status of JsonResponse in Django
0👍
- [Django]-Django rest framework: limit fields that can be updated
- [Django]-In the Django admin site, how do I change the display format of time fields?
- [Django]-How to export virtualenv?
Source:stackexchange.com