Django частичное обновление структуры фрейма с запросом where - PullRequest
0 голосов
/ 11 марта 2020

Я просто хочу обновить два поля таблицы, где id_payment = 1. Как это сделать? Это показывает мне ошибку ниже. IntegrityError at / receivePendingPayment / (1048, «Столбец« bank_id »не может быть нулевым»)

#View

    @permission_classes([IsAuthenticated])
    @api_view(['PATCH'])
    def receivePendingPayment(request):
        id_payment = request.data['id_payment']
        data = {'accountant_received_by': request.user.id, 'accounts_status': 'Received'}

        BankPaymentHistory.objects.filter(id=id_payment)
        serializer = BankPaymentUpdateSerializer(data=data, partial=True, many=False)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_200_OK)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    #Serializer

    class BankPaymentUpdateSerializer(serializers.ModelSerializer):
    class Meta:
        model = BankPaymentHistory
        fields = ('accounts_status','accountant_received_by')

    #Model

    class BankPaymentHistory(models.Model):
        activation_enum = (('Active', 'Active'), ('Deactive', 'Deactive'))
        accounts_status_enum = (('Pending', 'Pending'), ('Received', 'Received'))
        bank = models.ForeignKey(Bank, on_delete=models.CASCADE)
        project = models.ForeignKey(Project, on_delete=models.CASCADE)
        file = models.ForeignKey(File, on_delete=models.CASCADE)
        salesperson = models.ForeignKey(SalesPerson, on_delete=models.CASCADE)
        paymentmode = models.ForeignKey(PaymentModes, on_delete=models.CASCADE)
        amount = models.PositiveIntegerField()
        chequeno = models.CharField(max_length=50, unique=True)
        chequedate = models.DateField()
        paymentdate = models.DateField()
        remark = models.TextField(blank=True, null=True)
        user = models.ForeignKey(User, on_delete=models.CASCADE)
        accountant_received_by = models.ForeignKey(User, related_name='%(class)s_accountant_received_by', on_delete=models.CASCADE, null = True)
        accountant_received_date = models.DateTimeField(default=datetime.now)
        create_date = models.DateTimeField(default=datetime.now)
        activation_status = models.CharField(max_length=20, choices=activation_enum, default='Active')
        accounts_status = models.CharField(max_length=20, choices=accounts_status_enum, default='Pending')

1 Ответ

0 голосов
/ 11 марта 2020

Передайте экземпляр сериализатору, чтобы он мог знать, что он должен выполнить обновление.

 instance = BankPaymentHistory.objects.filter(id=id_payment).first()
 serializer = BankPaymentUpdateSerializer(instance=instance, data=data, partial=True, many=False)
...