-
Notifications
You must be signed in to change notification settings - Fork 25
Description
I try to create an object via a POST request. This works very well, now to my problem:
I want the response to have the field expanded by default (in the direct response to the post request).
When trying this either via http://localhost:8000/api/cities?expand=country
or setting extensions_expand = {'country'}
on the view (my preferred way), i get a validation error "country": ["This field is required." ]
.
So using extensions_expand={'country'}
results in an error on POST (and also PUT i think). I can bypass this error by using get_extensions_mixin_context
on the view
def get_extensions_mixin_context(self):
context = super(CityViewSet, self).get_extensions_mixin_context()
if self.request.method in ['POST', 'PUT']:
context['expand'] = set()
return context
This works at least, but still doesn't produce the desired result of having country
expanded in the post response.
I also have a repo ready for reproduction: https://github.com/BerniWittmann/drf_serializer_ext
Just try to post something to localhost:8000/api/cities
. The desired response would be:
{
"url": "http://localhost:8000/api/cities/1/",
"name": "Berlin",
"id": 1,
"country": {
"url": "http://localhost:8000/api/countries/1/",
"name": "Germany",
"id": 1,
"code": "DE"
},
"country_id": 1
}
If anything is unclear, let me know :)