1

Good day,

I would like to ask, if there's a possibility to gain additional data inside my serializers? These are my models...

models.py

class Chair(models.Model):
    name = models.CharField(max_length=100, null=False, blank=False, unique=True)
    bookable = models.BooleanField(default=False)
    user_created = models.CharField(max_length=100)
    date_created = models.DateField(auto_now_add=True)

class Booking(models.Model):
    chair = models.ForeignKey(Chair, on_delete=models.CASCADE)
    day = models.DateField()
    user_name = models.CharField(max_length=100)
    user_created = models.CharField(max_length=100)
    date_created = models.DateField(auto_now_add=True)

and these my serializers...

serializers.py

class BookingSerializer(serializers.ModelSerializer):
    class Meta:
        model = Booking
        fields = '__all__'


class ChairSerializer(serializers.ModelSerializer):
    class Meta:
        model = Chair
        fields = '__all__'

When making a request inside js like this...

views.py

@api_view(['GET'])
def bookings_by_date(request, pk):
    bookings = Booking.objects.filter(day=pk)
    serializer = BookingSerializer(bookings, many=True)
    return Response(serializer.data)

script.js

let url = '...here's my url for Booking...';

fetch(url)
.then((resp) => resp.json())
.then(function(data) {
    // do something here
});

...I would like to get not only the id of the Chair (models.Foreignkey), but also it's name. My first thought was doing something like this...

class ChairSerializer(serializers.ModelSerializer):
    class Meta:
        model = Chair
        fields = [
            ...
            'chair',
            'chair__name',
            ...
            ]

...but this doesn't seem to work! Does anyone know a solution for my problem? Thanks for all your help and have a great weekend!

1 Answer 1

2

You can use one of this two ways:

1-) Using SerializerMethodField. You can add readonly fields with this way. You should add get_<field_name> method or give a method name that you want to run for this field with name keyword. You can look the document for more details.

class BookingSerializer(serializers.ModelSerializer):
    chair__name = serializers.SerializerMethodField()

    class Meta:
        model = Booking
        fields = '__all__'

    def get_chair_name(self, obj):
        return obj.chair.name

2-) Using CharField with source attribute: You can define basically this field fill from where.

 class BookingSerializer(serializers.ModelSerializer):
    chair__name = serializers.CharField(source='chair__name')

    class Meta:
        model = Booking
        fields = '__all__'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.