Boris The Animal - 11 months ago 34

Python Question

I am calculating a field in my view file

`data_dict = { 'quantity_order': float(bom.quantity) * float(quantity)}`

I added float conversion since I was getting a different problem:

"can't multiply sequence by non-int of type 'Decimal'"

In model both fields are defined as decimal

`quantity_order = models.DecimalField(max_digits=19, decimal_places=3)`

one of the fields is received via url regular expression

`url(r'^production/order/new/(?P<pk_bom>\d+)/(?P<pk_soproduct>\d+)/(?P<uri>\S+)/(?P<quantity>\d+(\.\d{1,3}))/$',views.Production_order_new, name="production_order_new"),`

So the issue is that sometimes the calculation is not correct.

For example if both quantity values are

`44`

`44 * 666`

`443556.000`

Can it be the converting values to float before multiplication is causing this error?

Answer

"can't multiply sequence by non-int of type 'Decimal'"

What this actually means is that one of your operands is a sequence and the other is a Decimal. Ie, one of `bom.quantity`

or `quantity`

is a sequence and not an int, float or decimal.

```
data_dict = { 'quantity_order': float(bom.quantity) * float(quantity)}
```

A sequence can be multplied by an int.

```
seq = [1,2,3]
seq * 2 # produces [1,2,3,1,2,3]
```

but

```
seq * 2.5 # produces can't multiply sequence by non-int of type 'float'
```

Source (Stackoverflow)