Python 在Django Rest中保存十进制字段

Python 在Django Rest中保存十进制字段,python,testing,django-rest-framework,Python,Testing,Django Rest Framework,我正在尝试对我的项目进行测试,以创建发票,但在尝试保存实例时出现此错误 decimal.invalidoperation:[decimal.invalidoperation'>] 注意:当我用手扔网站时,我没有得到任何错误 在serializer.py中创建函数: def create(self, validated_data): product_data = validated_data.pop('product_data') invoice = super(Invoi

我正在尝试对我的项目进行测试,以创建
发票
,但在尝试保存实例时出现此错误

decimal.invalidoperation:[decimal.invalidoperation'>]

注意:当我用手扔网站时,我没有得到任何错误

在serializer.py中创建函数:

    def create(self, validated_data):
    product_data = validated_data.pop('product_data')
    invoice = super(InvoiceSerializer,self).create(validated_data)
    amount = 0
    for key, value in product_data.items():
        product = Products.objects.get(id=key)
        invoice_detail = InvoiceDetail.objects.create(invoice= invoice,
                                     product=product,
                                     product_description=product.description,
                                     product_price=product.unit_price,
                                     quantity_sold=value
                                     )
        invoice_detail.save()
        amount += Decimal(invoice_detail.product_price) *\
                  Decimal(invoice_detail.quantity_sold) * (1 - (product.discount / 100))

    amount *= Decimal(1 - (invoice.discount / 100))
    invoice.amount = amount
    invoice.remaining = amount
    invoice.save()
    return invoice
tests.py中的测试函数:

class EstimatedInvoiceAPITest(APITestCase):

    def setUp(self):
        self.user = User.objects.create(
            username='admin',
            email='admin@gmail.com.com',
            password='123456',
        )

        self.client = APIClient()
        self.client.force_authenticate(user=self.user)

        self.Customer = mommy.make(Customer)
        self.Customer.max_discount = 10
        self.Customer.save()
        self.Agent = mommy.make(Agent)
        self.Agent.max_discount = 5
        self.Agent.save()
        self.Product = mommy.make(Products, _quantity=2)
        print(self.Product[0].unit_price)
        self.data = {
            "invoice_number": "5",
            "customer": 1,
            "agent": 1,
            "invoice_due_date": "2017-02-05T00:00:00Z",
            "discount": 0,
            "type": "Credit",
            "product_data": {'1': '2', '2': '1'},
        }


    def test_calc_invoice_amount_and_remaining(self):
        self.client.login(username='admin', password='123456')
        url = '/sales/agentapi/invoices/'
        response = self.client.post(url, self.data, format='json')
        self.assertEquals(response.status_code, status.HTTP_201_CREATED)
models.py:

class Invoices(models.Model):
    invoice_number = models.CharField(max_length=100, unique=True)
    customer = models.ForeignKey(Customer)
    agent = models.ForeignKey(Agent)
    date = models.DateTimeField(auto_now_add=True)
    invoice_due_date = models.DateTimeField()
    amount = models.DecimalField(max_digits=9, decimal_places=2, null=True)
    discount = models.IntegerField()
    status = models.CharField(max_length=100, choices=Status, default='Open')
    type = models.CharField(max_length=100, choices=Type)
    remaining = models.DecimalField(max_digits=9, decimal_places=2, null=True)
    products = models.ManyToManyField('Products', through='InvoiceDetail')

    def __str__(self):
        return self.invoice_number

错误在于,由mommy生成的
Product.discount
字段是一个非常大的数字,因此它在等式中失败。。我手动分配此字段,一切正常

    self.Product = mommy.make(Products, _quantity=2, discount=10)