Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django overwriting data in mysql database

Hey I am creating a project in django where I keep track of orders. Now I want to add all the items to an orderline table to keep track of which items are ordered. But when I want to add multiple items from 1 order to the database it only enters the last one. My guess is that it overrides the previous one. This is my Orderline model class in django.

class OrderLine(models.Model):
product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True, blank=True)
order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True, blank=True)
quantity = models.IntegerField()
size = models.CharField(max_length=50)

def __str__(self):
    return self.to_dict()

def to_dict(self):
    return {
        'product': self.product.to_dict(),
        'order': self.order.to_dict(),
        'quantity': self.quantity,
        'size': self.size
    }

def add(self, product, order, quantity, size):
    self.product = Product.objects.get(id=product)
    self.order = Order.objects.get(id=order)
    self.quantity = quantity
    self.size = size
    self.save()
    return self.to_dict()

def add_orderline(self, product, order, quantity, size):
    if self.exists(product, order, size):
        orderline = OrderLine.objects.get(product=product, order=order, size=size)
        orderline.quantity += quantity
        orderline.save() 
        return orderline.to_dict()
    else:
        orderline = self.add(product, order, quantity, size)
        return orderline

def exists(self, product, order, size):
    return OrderLine.objects.filter(product=product, order=order, size=size).exists()

def get_all_by_order(self, order):
    return OrderLine.objects.filter(order=order)

This is the code that should create multiple orderlines.

for idx in range(len(cart_items)):
        orderline = order_line.add_orderline(cart_items[idx],uncomplete_order["id"], cart.get_by_user(body['user_id'])[idx]["quantity"], cart.get_by_user(body['user_id'])[idx]["size"])
        print(orderline)

NOTE: it is not important to know the data in the order_line.add_orderline() function i already checked the data and it is correct.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Now for my example I have 2 items in my cart_items but only the last one gets added. Im not sure if this is relevant information but I’m using a mysql database hosted on a lightsail server from AWS. if there is any more information you guys need, you can refer to it in the comments. All the help is greatly appreciated.

>Solution :

Your add method actually modifies an existing row and not create a new one. You probably want something like this:

@classmethod
def add(cls, product, order, quantity, size):
    obj = cls()
    obj.product = Product.objects.get(id=product)
    obj.order = Order.objects.get(id=order)
    obj.quantity = quantity
    obj.size = size
    obj.save()
    return obj.to_dict()

And then you should call it with OrderLine.add() to make it more clear you’re creating a new one and not doing something with an existing object.

You’re add_orderline is also being called on an existing object and self will point to that existing object. Maybe that should also be a @classmethod?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading