A
A
abramoov2019-03-02 02:47:56
Django
abramoov, 2019-03-02 02:47:56

I need help, I don’t understand why I don’t see total_price?

from django.db import models
from products.models import Product
from django.db.models.signals import post_save


class Status(models.Model):
    name = models.CharField(max_length=24, blank=True, null=True, default=None)
    is_active = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __str__(self):
        return "Статус %s" % self.name

    class Meta:
        verbose_name = "Статус"
        verbose_name_plural = "Статусы заказа"


class Order(models.Model):
    total_price = models.DecimalField(max_digits=10, decimal_places=2, default=0)
    customer_name = models.CharField(max_length=64, blank=True, null=True, default=None)
    customer_email = models.EmailField(blank=True, null=True, default=None)
    customer_phone = models.CharField(max_length=48, blank=True, null=True, default=None)
    customer_address = models.CharField(max_length=128, blank=True, null=True, default=None)
    comments = models.TextField(blank=True, null=True, default=None)
    status = models.ForeignKey(Status, on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __str__(self):
        return "Заказ %s  %s" % (self.id, self.status.name)

    class Meta:
        verbose_name = "Заказ"
        verbose_name_plural = "Заказы"

    def save(self, *args, **kwargs):

        super (Order, self).save(*args, **kwargs)


class ProductInOrder(models.Model):
    order = models.ForeignKey(Order, blank=True, null=True, default=None, on_delete=models.CASCADE)
    product = models.ForeignKey(Product, blank=True, null=True, default=None, on_delete=models.CASCADE)
    nmb = models.IntegerField(default=1)
    price_per_item = models.DecimalField(max_digits=10, decimal_places=2, default=0)
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, default=0)#ЦЕНА НА КОЛВО
    is_active = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __str__(self):
        return "%s" % self.product.name

    class Meta:
        verbose_name = "Товар в заказе"
        verbose_name_plural = "Товары в заказе"

    def save(self, *args, **kwargs):
        price_per_item = self.product.price
        self.price_per_item = price_per_item
        self.total_price = self.nmb * self.price_per_item

        super (ProductInOrder, self).save(*args, **kwargs)

def product_in_order_postsave(sender, instance, created, **kwargs):
    order = instance.order
    all_product_in_order = ProductInOrder.objects.filter(order=order, is_active=True)
    order_total_price = 0
    for item in all_product_in_order:
        order_total_price += item.total_price

    instance.order.total_price = order_total_price
    instance.order.save(force_update=True)

post_save.connect(product_in_order_postsave, sender=ProductInOrder)

Exception Value: 'ProductInOrder' object has no attribute 'total_price'
Exception Location: C:\Users\1\PycharmProjects\upitt\orders\models.py in product_in_order_postsave, line 73
why doesn't it see total_price?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
tema_sun, 2019-03-02
@abramoov

It's written right there.
Instead:
it should probably be:
And although you are again trying to reach item.total_price in the signal.
I do not quite understand the meaning of what you want to do, but note that ProductInOrder has no total_price defined anywhere, only Order has it.

M
Mikhail Enikeev, 2016-10-11
@ferdasfarmazone

Install node.js on your computer

I
Irina, 2016-11-13
@MissIrina

Just try installing Node.js directly from here Node.js site and reboot your system.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question