python - Django Rest Set some new value on django view on Post -
my form post data db.
i want add 1 filed hidden in view , should generate key random.
my post function is
def post(self, request, format=none): request.data['code'] = id_generator() data = request.data serializer = registrationform(data=data) if serializer.is_valid(): serializer.save() x=serializer.data data = { "ack":"success"} return response(data, status=status.http_201_created) return response(serializer.errors, status=status.http_400_bad_request)
here id_generator() genrate random values.
in serializer
class regiform(serializers.modelserializer): code = serializers.charfield(read_only=true) class meta: fields = ( 'mobile', 'profile_image', 'code') model = registration
so code filed not showing in api view , submitting form post code still empty
the problem setting read , docs says not able used set values:
set true ensure field used when serializing representation, not used when creating or updating instance during deserialization.
so if want hidden can use in charfield style tag allowed like: serializers.charfield(style={'input_type':'hidden'})
advice: should use hiddenfield instead of charfield hiddenfield takes value callable or default value seems want do.
Comments
Post a Comment