# updating DecimalField with value received from other model

**URL:** <https://forum.djangoproject.com/t/updating-decimalfield-with-value-received-from-other-model/24779>\
**Category:** Using the ORM\
**Created:** [October 25, 2023, 3:15pm UTC](https://forum.djangoproject.com/t/updating-decimalfield-with-value-received-from-other-model/24779 "2023-10-25T15:15:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dariofiru](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dariofiru/32/16608_2.png) [@dariofiru](https://forum.djangoproject.com/u/dariofiru)\
**Post date:** [October 25, 2023, 3:15pm UTC](https://forum.djangoproject.com/t/updating-decimalfield-with-value-received-from-other-model/24779/1 "2023-10-25T15:15:04Z")

</div>

Hi all,

I have the following models:

```auto
  class Sale(models.Model):
     location= models.CharField(max_length=300)
     price = models.DecimalField(decimal_places=2, max_digits=12)
  
  class Product(models.Model):
     product = models.CharField(max_length=300)
     price = models.DecimalField(decimal_places=2, max_digits=12)

```

that I’m trying to update with:

```auto
      discounted_price = Product.objects.filter(product=product).first() 
      completed_sale= Sale.objects.filter(id=id).first()
      best_price = getattr(discounted_price, "price") 
      
      completed_sale.price =best_price 
      completed_sale.save(update_fields=["price "]) 

```

Basically I’m retrieving the price from a product and updating the sale price, both models have a price field of type `DecimalField(decimal_places=2, max_digits=12)` but I can’t manage to update the sale price with `completed_sale.price =best_price `

IT should be pretty straightforward but the field is not updated. I have also some other text fields in the 2 models that are updated without any issues following this structure.

Do I have to somehow cast the DecimalField or am I missing something?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 25, 2023, 4:12pm UTC](https://forum.djangoproject.com/t/updating-decimalfield-with-value-received-from-other-model/24779/2 "2023-10-25T16:12:32Z")

</div>

Side note: When posting blocks of code here, surround the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. Also, for snippets on a single line, you can enclose the snippet between single backtick characters. (I’ve taken the liberty of editing your original post.)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 25, 2023, 4:22pm UTC](https://forum.djangoproject.com/t/updating-decimalfield-with-value-received-from-other-model/24779/3 "2023-10-25T16:22:00Z")

</div>

Couple items.

- Instead of:

> [@dariofiru](#):
>
> `best_price = getattr(discounted_price, "price")`

you can use:  
`best_price = discounted_price.price`

- You have a space in the update\_fields parameter:

> [@dariofiru](#):
>
> ```auto
> completed_sale.save(update_fields=["price "])
> ^
> 
> ```

I am not sure that that is the cause of the issue, but it’s still worth fixing.

- In both cases where you’re using the expression `.filter(...).first()`, you’re a lot better off using `.get(...)`, especially when there should only be one instance retrieved. (You should be handling a null response either way, so it’s not like this is saving you any code.)

Beyond these items, if you are getting an error, please post the full error and traceback here. (also enclosed between lines of three backtick characters)

---

<div class="post-metadata">

**Author:** ![dariofiru](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dariofiru/32/16608_2.png) [@dariofiru](https://forum.djangoproject.com/u/dariofiru)\
**Post date:** [October 26, 2023, 11:12am UTC](https://forum.djangoproject.com/t/updating-decimalfield-with-value-received-from-other-model/24779/4 "2023-10-26T11:12:17Z")

</div>

Thanks a lot! I switched to

```auto
Sales.objects.filter(id=id).update(sales_status="sold", etc... )

```

and it works!
