Django:查询不同模型的方法

时间:2019-04-10 20:19:33

标签: django

我有一个模型CartItem,它具有一个Product模型的外键。

由于采用了Product模型,我得到了descriptionimage等。

但是,我想要一个名为sub_total的方法,该方法返回和整数。我用它来计算为此CartItem支付的总额。

此sub_total方法使用costo_de_los_productos的某些属性查询其他模型CartItem。像:self.product.category.nameself.product.nameself.sizeself.quantity

  

我需要从sub_total方法返回一个整数。

但是,我的查询不正确,如果我对其进行注释并返回0,则该查询有效,但总计为0。

def sub_total(self):    
  product_price = costo_de_los_productos.objects.filter(category=self.product.category.name,    
  product = self.product.name,
  size=self.size,
  quantity=self.quantity).values_list("price", flat=True)

怎么了?

class CartItem(models.Model):
    cart = models.ForeignKey(Cart, on_delete=models.CASCADE)
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    size = models.CharField(max_length=20, choices=TAMANIOS)
    quantity = models.CharField(max_length=20, choices=CANTIDADES)
    file = models.FileField(upload_to='files', blank=True, null=True)
    comment = models.CharField(max_length=100, blank=True, null=True, default='')
    uploaded_at = models.DateTimeField(auto_now_add=True)
    step_two_complete = models.BooleanField(default=False)

    # def __str__(self):
    #     return str(self.id) + " - " + str(self.size) + " por " + str(self.quantity)

    def sub_total(self):
        product_price = costo_de_los_productos.objects.filter(category = self.product.category.name,
                                         product = self.product.name,
                                         size=self.size,
                                         quantity=self.quantity).values_list("price", flat=True)
        # print(type(product_price))
        return product_price

costo_de_los_productos模型:

class costo_de_los_productos(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    price = models.IntegerField(default=30)
    size = models.CharField(max_length=20, choices=TAMANIOS)
    quantity = models.CharField(max_length=20, choices=CANTIDADES)

产品型号:

class Product(models.Model):
    name = models.CharField(max_length=250, unique=False)
    slug = models.SlugField(max_length=250, unique=False)
    description = models.TextField(blank=True)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    image = models.ImageField(upload_to='product', blank=True, null=True)
    available = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ('name',)
        verbose_name = 'product'
        verbose_name_plural = 'products'

    def get_url(self):
            return reverse('shop:ProdDetail', args=[self.category.slug, self.slug])

    def __str__(self):
        return '{}'.format(self.name)

类别模型:

class Category(models.Model):
    name = models.CharField(max_length=250, unique=True)
    slug = models.SlugField(max_length=250, unique=True)
    description = models.TextField(blank=True, null=True)
    image = models.ImageField(upload_to='category', blank=True, null=True)
    video = EmbedVideoField(null=True, blank=True)

    class Meta:
        ordering = ('name',)
        verbose_name = 'category'
        verbose_name_plural = 'categories'

    def get_url(self):
        return reverse('shop:allCat', args=[self.slug])

    def __str__(self):
        return '{}'.format(self.name)

管理面板中“ costo_de_los_productos”的图片:

enter image description here

更新1

在product_price查询之后无法打印任何内容。

def sub_total(self):
    print("Enters Subtotal")
    print(self.product.category.name)
    print(self.product.name)
    print(self.size)
    print(self.quantity)
    product_price = costo_de_los_productos.objects.filter(category=self.product.category.name,
                                                          product=self.product.name,
                                                          size=self.size,
                                                          quantity=self.quantity).values_list("price", flat=True)[0]

    print("Line after product_price query")
    print(type(product_price))

    return product_price

对值进行硬编码不会返回预期的整数:

def sub_total(self):
    print("Enters Subtotal")
    print(self.product.category.name)
    print(self.product.name)
    print(self.size)
    print(self.quantity)
    product_price = costo_de_los_productos.objects.filter(category="Stickers",
                                                          product="Stickers transparentes",
                                                          size="5cm x 5cm",
                                                          quantity=300).values_list("price", flat=True)[0]

    print("Line after product_price query")
    print(type(product_price))

    return product_price

打印结果:

Enters Subtotal
Stickers
Stickers transparentes
5cm x 5cm
300

0 个答案:

没有答案