'I am making POST request from flutter app to Django-Rest user and getting null response

I'm a novice at this. I have been making POST requests from my flutter app to Django-Rest user and getting a null response on vendor name as copy pasted below. Other parts of the the json body are posting normally except for the user that the POST request is directed to. I want to be able to send the order from the flutter app to the particular user that posts the stock product. When I post I am able to send all the details such as name and number,but the vendor name gives that null response. I have tried googling but I don't understand what is wrong with the code

this is the json response:

 {
        "vendor": null,
        "name": "Test",
        "number": "072345678",
        "address": "Linc street",
        "size": "3kgs",
        "note": "4th floor"
    }, ``

The Post request in Flutter:


Future<Order> createOrder(vendor, String name, String number, String address, String size, String note) async 
{
    final String apiUrl = 'http://10.0.2.2:8000/Order/';

    final response = await http.post(apiUrl, body:{

      vendor: vendor,
      "name": name,
      "number": number,
      "address": address,
      "size": size,
      "note": note

    });

    if(response.statusCode == 201){
      final String responseString = response.body;

      return orderFromJson(responseString); 
    }else{
      return null;
    }
  }

and

Order orderFromJson(String str) => Order.fromJson(json.decode(str));

String orderToJson(Order data) => json.encode(data.toJson());

class Order {
    Order({
        this.vendor,
        this.name,
        this.number,
        this.address,
        this.size,
        this.note,
    });
    
    String vendor;
    String name;
    String number;
    String address;
    String size;
    String note;

    factory Order.fromJson(Map<String, dynamic> json) => Order(
        vendor: json["vendor"],
        name: json["name"],
        number: json["number"],
        address: json["address"],
        size: json["size"],
        note: json["note"],
    );

    Map<String, dynamic> toJson() => {
        "vendor": vendor,
        "name": name,
        "number": number,
        "address": address,
        "size": size,
        "note": note,
    };
}

My django Models and serializers are:

from django.db import models

from django.contrib.auth.models import User

from django.contrib.auth import get_user_model

# Create your models here.

class Stock(models.Model):
    vendor = models.ForeignKey(User, default=None, on_delete=models.CASCADE, null=True)
    gastype = models.CharField(max_length= 20,null=True)
    image = models.ImageField(null=True, upload_to="media/stockimages")
    price = models.CharField(max_length=30,null=True)

    def __str__(self):
        return str(self.vendor) if self.vendor else ''


class Order(models.Model):
    vendor = models.ForeignKey(Stock, default=None , on_delete=models.CASCADE, null=True)
    name = models.CharField(max_length=50,null=True)
    number = models.CharField(max_length=150,null=True)
    address = models.CharField(max_length=50,null=True)
    size = models.CharField(max_length=50,null=True)
    note = models.CharField(max_length=150,null=True)

    def __str__(self):
        return self.name


and

class OrderSerializer(serializers.HyperlinkedModelSerializer):

    def perform_create(self, serializer):
        vendor = vendor.objects.get(user=self.request.user)
        return serializer.save(vendor=vendor)

    class Meta:
        model = Order
        fields = ('vendor','name', 'number','address','size','note')

class StockSerializer(serializers.HyperlinkedModelSerializer):
    vendor = serializers.StringRelatedField(read_only=True)

    class Meta:
        model = Stock
        fields = ('vendor', 'gastype', 'image','price')

also,my views and viewsets are for the same are below:

views:

def manage_orders(request):

    def pre_save(self, obj):
        vendor = request.user
        return serializer.save(vendor=vendor)
        Order = self.request.user

    orders = Order.objects.filter(vendor=request.user)
    return render(request,'orders.html',{"orders": orders})

viewsets:

class OrderViewSet(viewsets.ModelViewSet):
    queryset = Order.objects.all().order_by('name')
    serializer_class = OrderSerializer


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source