[Django]-Django: 'int' object is not iterable when different serializers in a Viewset

2👍

You can’t force ModelViewset to return a hotel for retreive actions when it is configured for a vacancy model.

By specifying HotelDetailsSerializer there you produce an error cause when you fetch /api/vacancies/1/ it is a vacancy instance with ID=1 that’s being passed to that serialzier which is obviously not right, you should only pass hotel to HotelDetailsSerializer.

Now, a hotel can have multiple vacancies assigned to it (you showed it yourself in /api/hotels/1 response). So if you want to fetch stocks by hotel_id then a request should return multiple vacancies for that bar, not 1.

An API to fetch multiple vacancies is GET /api/vacancies/, all you need is to pass a query param to filter it like this: GET /api/vacancies/?hotel=1

You can achieve it with django-filter

After installing it as stated in docs, add it to your VacancyViewSet:

filterset_fields = ['hotel']
👤GProst

Leave a comment