I have url like doc/<int:doc_id>/price/ for get and post and doc/<int:doc_id>/price/<int:id> for delete and patch or retrieve. My problem is, when creating a new price I want to set price.doc_id=self.kwargs[‘doc_id’] but at the same time I want that serializer field to be hidden in swagger documentation(I use drf-spectacular). The end-user should not be aware of that field.
I know I can overwrite the create function of serializer and explicitly set the price or use serializers.save(doc=doc). But I ask this question to get a better solution. I tried hiddenfield as well but apparently it only takes default value and I do not know how to pass doc_id to it.
class DocPriceOutHistorySerializer(ModelSerializerWithValidator):
currency_name = serializers.CharField(read_only=True)
type_price_name = serializers.CharField(read_only=True)
class Meta:
model = models.DictPriceList
fields = (
'id',
'dat',
'price',
'currency',
'currency_name',
'type_price',
'type_price_name',
'obj',
'doc',
)
and in views.py
data = request.data
data['doc'] = self.kwargs['doc_id']
serializer = self.get_serializer(data=data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
this will work but I do not want to manually write a request schema for swagger.
Aykhan Aghayev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.