0

serializer.py

class DataSerializer(serializers.ModelSerializer):

    flag = serializers.BooleanField(required=False)
    class Meta:
        model = Plans
        fields = {
            'id',
            'name',
            'type',
            'details',
            'price'
        }

views.py

class DetailsView(APIView):
    def get(self, request):
        user_plans = Order.objects.filter(id=request.user).first()
        selected_plan = False
        if user_plans is not None:
            selected_plan = True
            original_plans = Plans.objects.all()
            user_serializer = DataSerializer(original_plans, many=True)
            return Response(user_serializer.data)

These are my serializers and views.

I want to send selected_plan into my serializers as an output. I have created an extra field in serializers called flag for this but don't know how to send it. Can anyone help?

{
  'id':1
  'name':'Name1'
  'type':'normal'
  'details':'NA'
  'price':2563,
  'flag':True
},
{
  'id':2
  'name':'Name2'
  'type':'normal'
  'details':'NA'
  'price':5200,
  'flag':True
}

This is the what i am looking for

ReaL_HyDRA
  • 314
  • 1
  • 18
  • What do you mean by *"send"* here? Since you're using a `get(...)` (or `HTTP GET`) method, you don't seem to have a payload from the client. – JPG Nov 23 '22 at 03:46
  • Did you mean to return some data from Django to the client with the field `selected_plan`? – JPG Nov 23 '22 at 03:47
  • `{ 'id':1 'name':'Name1' 'type':'normal' 'details':'NA' 'price':2563, 'flag':True }` This is a format that expects, but now it is not. Pardon my question-asking abilities This is my first time working on DRF – ReaL_HyDRA Nov 23 '22 at 03:55
  • What could be the value of `selected_plan`? – JPG Nov 23 '22 at 03:57
  • It is a boolean field. value can change to true of false. – ReaL_HyDRA Nov 23 '22 at 04:05

0 Answers0