How can I change the html form fields default mapping, to populate its value from the response?

187 views
Skip to first unread message

Manish Shah

unread,
Aug 10, 2021, 2:12:29 AM8/10/21
to Django REST framework
Greetings everyone,

I have these nested serializers, in which the fields are not appropriately maped to html form fields in default browsable API page of django rest_framework, I have customized the representation of the response, and now I want to map those values to the appropriate HTML form fields

Below is my `serializer`
``` python
from rest_framework_gis import serializers as gis_serializers
from rest_framework import serializers

class NestedtLocationSerializer(gis_serializers.GeoFeatureModelSerializer):
    class Meta:
        model = Location
        geo_field = 'geometry'
        fields = (
            'type',
            'is_mobile',
            'name',
            'address',
            'geometry',
        )


class NestedFloorplanSerializer(serializers.ModelSerializer):
    class Meta:
        model = FloorPlan
        fields = (
            'floor',
            'image',
        )


class DeviceLocationSerializer(serializers.ModelSerializer):
    location = NestedtLocationSerializer()
    floorplan = NestedFloorplanSerializer(required=False, allow_null=True)

    class Meta:
        model = DeviceLocation
        fields = (
            'location',
            'floorplan',
            'indoor',
        )
```

with the above serilaizers, I am getting this:-
Response:-
NbOXd.png


But in the html form fields the values of `is_mobile`, `type`, `name` are not loading in the html form fields, (probably because the representation of the data has been customized):-

5AtrZ.png

Is there any way, I can control the default mapping of these values to appropriate serializer fields to fill the values in the form in the django-rest-framework browsable page??

Abeer Eltanawy

unread,
Aug 10, 2021, 6:48:32 AM8/10/21
to django-res...@googlegroups.com
You will need to loop over the response values using map and assign each value to their designated form field.

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/100f6a81-6293-4937-8ffb-73dc81a72401n%40googlegroups.com.

Manish Shah

unread,
Aug 10, 2021, 10:30:49 AM8/10/21
to Django REST framework
Hi, thanks for the answer. This form in the screenshot has not been made by me it is the default Browsable Page of rest_framework which maps the serializer fields to fill in the value to it, I was thinking of some generic way like having a method or option to manipulate how the fields value are rendered, to fill in the value!!!
Reply all
Reply to author
Forward
0 new messages