获取并使用外键字段中的值,无需任何转换(Django)

发布于 2025-01-16 12:10:25 字数 1276 浏览 2 评论 0原文

我尝试从“priceWithTax()”中的外键字段“tax”获取值,最后,我可以使用下面的代码获取该值:

taxObject = self.tax

这是完整的代码:

“models.py”

from django.db import models
from decimal import Decimal

class Tax(models.Model):
    tax = models.DecimalField(
        max_digits=3, 
        decimal_places=0,
        validators=[
            MaxValueValidator(100),
            MinValueValidator(0)
        ],
    )

class Product(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=5, decimal_places=2)

    TAX_ID = 1
    tax = models.ForeignKey(
        Tax,
        on_delete=models.PROTECT,
        default=TAX_ID,
    )

    @property
    def priceWithTax(self):
        # Here
        taxObject = self.tax
        tax = Decimal(str(taxObject))

        return round(self.price * ((tax + 100) / 100), 2)

但要使用该值,我需要先将其转换为“字符串”类型,然后再将其转换为“十进制类型”代码有点长,如下所示:

taxObject = self.tax
tax = Decimal(str(taxObject)) # Here

那么,有什么办法要使用“priceWithTax()”中外键字段“tax”的值而不进行任何转换?

I tried to get the value from the foreign key field "tax" in "priceWithTax()", then finally, I could get the value with this code below:

taxObject = self.tax

This is the full code:

"models.py":

from django.db import models
from decimal import Decimal

class Tax(models.Model):
    tax = models.DecimalField(
        max_digits=3, 
        decimal_places=0,
        validators=[
            MaxValueValidator(100),
            MinValueValidator(0)
        ],
    )

class Product(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=5, decimal_places=2)

    TAX_ID = 1
    tax = models.ForeignKey(
        Tax,
        on_delete=models.PROTECT,
        default=TAX_ID,
    )

    @property
    def priceWithTax(self):
        # Here
        taxObject = self.tax
        tax = Decimal(str(taxObject))

        return round(self.price * ((tax + 100) / 100), 2)

But to use the value, I needed to convert it to "String" type first and then "Decimal type" next which became a little bit long code as shown below:

taxObject = self.tax
tax = Decimal(str(taxObject)) # Here

So, are there any ways to use the value from the foreign key field "tax" in "priceWithTax()" without any conversions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稀香 2025-01-23 12:10:25

是的,您可以使用“priceWithTax()”中外键字段“tax”的值,无需任何转换

因此,替换此代码:

taxObject = self.tax
tax = Decimal(str(taxObject))

使用此代码:

tax = self.tax.tax

这是完整的代码:

"models.py"

from django.db import models
from decimal import Decimal

class Tax(models.Model):
    tax = models.DecimalField(
        max_digits=3, 
        decimal_places=0,
        validators=[
            MaxValueValidator(100),
            MinValueValidator(0)
        ],
    )

class Product(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=5, decimal_places=2)

    TAX_ID = 1
    tax = models.ForeignKey(
        Tax,
        on_delete=models.PROTECT,
        default=TAX_ID,
    )

    @property
    def priceWithTax(self):
        # Here
        tax = self.tax.tax        
        return round(self.price * ((tax + 100) / 100), 2)

Yes, you can use the value from the foreign key field "tax" in "priceWithTax()" without any conversions.

So, replace this code:

taxObject = self.tax
tax = Decimal(str(taxObject))

With this code:

tax = self.tax.tax

This is the full code:

"models.py":

from django.db import models
from decimal import Decimal

class Tax(models.Model):
    tax = models.DecimalField(
        max_digits=3, 
        decimal_places=0,
        validators=[
            MaxValueValidator(100),
            MinValueValidator(0)
        ],
    )

class Product(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=5, decimal_places=2)

    TAX_ID = 1
    tax = models.ForeignKey(
        Tax,
        on_delete=models.PROTECT,
        default=TAX_ID,
    )

    @property
    def priceWithTax(self):
        # Here
        tax = self.tax.tax        
        return round(self.price * ((tax + 100) / 100), 2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文