简体   繁体   中英

Django REST generic ListCreateView GET request is working but POST request is not working

I created a generic ListCreateAPI view. I am able to execute a GET request to the view but not a POST request.

Here is my model:

class InvoiceEntry(models.Model):

    SERVICE_OR_EXPENSE_CHOICE = [('SER', 'Service'),('EXP', 'Expense')]
    TYPE_CHOICE = [('BPA', 'BPA'),('CAE', 'CAE'),('MEC', 'MEC'),('TRA', 'Travel')]

    invoice_number = models.CharField(max_length=20)
    description = models.CharField(max_length=200)
    rate = models.FloatField()
    units = models.FloatField()
    discount = models.FloatField(default=0)
    amount = models.FloatField()
    service_or_expense = models.CharField(max_length=10, choices=SERVICE_OR_EXPENSE_CHOICE)
    type = models.CharField(max_length=10, choices=TYPE_CHOICE)

    def __str__(self):
        return '[' + str(self.invoice_number) + '][' + self.description + '][' + self.service_or_expense  + '][' + self.type + ']'

Here is my view:

class InvoiceEntryListCreate(ListCreateAPIView):

    queryset = InvoiceEntry.objects.all()
    serializer_class = InvoiceEntrySerializer

And here is my serializer:

class InvoiceEntrySerializer(serializers.ModelSerializer):

  class Meta:
    model = InvoiceEntry
    fields = ['invoice_number', 'description', 'rate', 'units', 'discount', 'amount', 'service_or_expense', 'type']

I get the following error when trying a post request:

{ "detail": "JSON parse error - Expecting value: line 1 column 1 (char 0)" }

在此处输入图像描述

Like Abdul and Carlos mentioned, it needs to be submitted as a JSON payload

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM