How to make (if & else) for internal values in django orm?

Question:

I am making models for my store project and I wanted to know why the code I wrote is wrong? And how can I write correctly?
I want only the time when a product is sold from me to be recorded in the database.

class Product(models.Model):
    product_type = models.ForeignKey(ProductType, on_delete=models.PROTECT, related_name='products_types')
    upc = models.BigIntegerField(unique=True)
    title = models.CharField(max_length=32)
    description = models.TextField(blank=True)
    category = models.ForeignKey(Category, on_delete=models.PROTECT, related_name='products')
    brand = models.ForeignKey(Brand, on_delete=models.PROTECT, related_name='products')

    soled = models.BooleanField(default=False)
    if soled == True:
        soled_time = models.DateTimeField(auto_now_add=True)


    created_time = models.DateTimeField(auto_now_add=True)
    modified_time = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.title

I hope that my problem will solve the question of many friends <3

Asked By: kambiz taherinia

||

Answers:

welcome to SO. Great question, let me explain.

Your model is correct, aside from the soled_time field, and the if statement above it.

That being said, I think you may be missing some logic here, unless you have some kind of single item single sale thing going on (as opposed to a stock of items) then you may need to add another model.

The product tends to be its own entity in this kind of example and we would create supporting models with relationships to the Product model.

For example, you may have a Cart model, which looks something like:

class Cart(models.Model):
    products = models.ManyToManyField(Product, related_name='carts_in', on_delete=models.CASCADE)
    # add fk to user, so we know who ordered
    # add some functions which sum the total cost of products
    

I hope this kind of makes sense, and then we would move the soled_time field away from products and onto Cart, because then we know when cart was sold, which items were in the cart, and which user made the cart.

Please also consider looking into existing diango packages which manage A LOT of the heavy lifting when it comes to e-commerce, I would Google and sniff around a bit and see if any of the existing packages suit your needs first, most of them are quite extendable aswell so you should be able to make it work for your use case even if it doesn’t fit right out of the box.

If you have any questions or I can add clarity to my answer, please feel free to add a comment, or if this satisfies your question, then dont forget to accept this answer and leave an upvote if you like.

As for the question of "How do I update the field when the item is sold?"

The answer is you override the save function of the model, like so:

class Cart(models.Model):
    products = models.ManyToManyField(Product, related_name='carts_in', on_delete=models.CASCADE)
    soled_time = models.DatetimeField()
    # add fk to user, so we know who ordered
    # add some functions which sum the total cost of products

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)
        if self.sold:
            self.sold_time = timezone.now()
Answered By: Swift