user6589133
user6589133

Reputation:

How to represent different values for GET and POST methods in one serializer field?

How to change the Representation of Serializer for GET and POST Request? Currently I am using The StringRelatedField in HotelSerializer which allows only readonly Feld I am unable to POST any Data because StringRelatedField does not maps with id in My case However id is manadotory For POST request.

If i use PrimaryKeyRelatedField for location in HotelSerializer than POST request will be executed and object will be saved Successfully

But hitting the api for GET request For a Hotel with id 1 the response will be

{ "id": 1, "name": "asasa", "discription": "sasas", "location": 1, } 

but i wanted the response should include the city name not the id of Location When i make GET Request

{ "id": 1, "name": "asasa", "discription": "sasas", "location": "delhi", }

or

{ "id": 1, "name": "asasa", "discription": "sasas","location": "Mumbai", } 

models

class Location(models.Model):
    country =  models.CharField(max_length=60)
    city = models.CharField(max_length=60,db_index=True)
    zipcode = models.CharField(max_length=10
class Hotel(models.Model):
    name =models.CharField(max_length=200,db_index=True)
    discription = models.TextField(blank=True,null=True)
    location = models.ForeignKey(Location,related_name='hotels')

serializer

class HotelSerializer(serializers.ModelSerializer):
    location=serializers.StringRelatedField() #this line Cause Problem

    class Meta:
        model = Hotel
        fields= ('id','name','discription','location',)

Upvotes: 0

Views: 2182

Answers (2)

Ivan Semochkin
Ivan Semochkin

Reputation: 8907

For your case you don't need any special field, DRF will provide PrimaryKeyRelateField by default for you:

class HotelSerializer(serializers.ModelSerializer):

    class Meta:
        model = Hotel
        fields= ('id','name','discription','location',)

Also all fields in Django by default use not null constrain, e.g null=False. If you want to save a null value you should explicit write it in a field:

class Hotel(models.Model):
    location = models.ForeignKey(Location,related_name='hotels', null=True, blank=True)

null=True allows you to save a null value and blank=True make this field not required in a form validation.
UPDATE:
If you want to change representation of a serializer fields you could override to_representation method:

class HotelSerializer(serializers.ModelSerializer):

    class Meta:
        model = Hotel
        fields= ('id','name','discription','location')

    def to_representation(self, instance):
        representation = super(HotelSerializer, self).to_representation(instance)
        representation['location'] = instance.location.city
        return representation

Upvotes: 4

Marin
Marin

Reputation: 1121

models

location = models.ForeignKey(Location, null=True, blank=True)

serializer

class HotelSerializer(serializers.ModelSerializer):
    location=serializers.PrimaryKeyRelatedField(queryset=Location.objects.all(), required=False) #this line Cause Problem

    class Meta:
        model = Hotel
        fields= ('id','name','discription','location',)

Upvotes: 1

Related Questions