0

I have a django table named Inventory

Models.py

class ClientInventory(models.Model):
    product_short_code = models.CharField(max_length=500, default=0, null=True)
    product_quantity = models.IntegerField(default=0, null=True)
    product_owner = models.ForeignKey(Client, on_delete=models.CASCADE, related_name='inventory_owner')

and I want to update this inventory whenever a form named Delivered Docket is filled and this is my view to do that

Views.py

@method_decorator([login_required, employee_required], name='dispatch')
class DeliveredDocketFormView(CreateView):
    model = DeliveredDocket
    fields = "__all__"
    template_name = 'packsapp/employee/docketDeliveredForm.html'

    def form_valid (self, form):
        product = form.save(commit=False)
        product.save()

        data = form.cleaned_data

        ClientInventory.objects.update_or_create(product_short_code=data["product1"], product_quantity =data["product1_recieved_quantity"],
                                                 product_owner = data['created_for'])
        ClientInventory.objects.update_or_create(product_short_code=data["product2"],
                                                 product_quantity=data["product2_recieved_quantity"],
                                                 product_owner=data['created_for'])
        ClientInventory.objects.update_or_create(product_short_code=data["product3"],
                                                 product_quantity=data["product3_recieved_quantity"],
                                                 product_owner=data['created_for'])
        ClientInventory.objects.update_or_create(product_short_code=data["product4"],
                                                 product_quantity=data["product4_recieved_quantity"],
                                                 product_owner=data['created_for'])
        ClientInventory.objects.update_or_create(product_short_code=data["product5"],
                                                 product_quantity=data["product5_recieved_quantity"],
                                                 product_owner=data['created_for'])
        ClientInventory.objects.update_or_create(product_short_code=data["product6"],
                                                 product_quantity=data["product6_recieved_quantity"],
                                                 product_owner=data['created_for'])
        ClientInventory.objects.update_or_create(product_short_code=data["product7"],
                                                 product_quantity=data["product7_recieved_quantity"],
                                                 product_owner=data['created_for'])
        ClientInventory.objects.update_or_create(product_short_code=data["product8"],
                                                 product_quantity=data["product8_recieved_quantity"],
                                                 product_owner=data['created_for'])

        messages.success(self.request, 'The Delivered Docket was created with success!')
        return redirect('employee:delivered_docket_table')

How can I reduce the number of ORM calls as this does not feel right??

This is the form data:

{'sender': <Warehouse: Yantra Gurgaon Warehouse>, 'pending_docket_list': <AllotmentDocket: Yantra Gurgaon Warehouse::client::2019-12-04>, 'material_received_date': datetime.date(2019, 12, 4), 'pod
_received': 'Yes', 'product1': 'Vipin', 'product1_alloted_quantity': 56, 'product1_recieved_quantity': 56, 'product2': None, 'product2_alloted_quantity': None, 'product2_recieved_quantity': None, 'product3': No
ne, 'product3_alloted_quantity': None, 'product3_recieved_quantity': None, 'product4': None, 'product4_alloted_quantity': None, 'product4_recieved_quantity': None, 'product5': None, 'product5_alloted_quantity':
 None, 'product5_recieved_quantity': None, 'product6': None, 'product6_alloted_quantity': None, 'product6_recieved_quantity': None, 'product7': None, 'product7_alloted_quantity': None, 'product7_recieved_quanti
ty': None, 'product8': None, 'product8_alloted_quantity': None, 'product8_recieved_quantity': None, 'created_on': datetime.datetime(2019, 12, 4, 18, 35, 29, tzinfo=<UTC>), 'created_for': <Client: client>, 'rema
rks': '65'}

Also when I enter the same short name in the form as I have entered previously with a different quantity instead of modifying it, it creates a new entry. What I want is that whenever someone enters the same short name then it should sum the new quantity with the previous quantity, How can I do that ?

1 Answer 1

2

To reduce the number of ORM calls, why not do something like this -

@method_decorator([login_required, employee_required], name='dispatch')
class DeliveredDocketFormView(CreateView):
    model = DeliveredDocket
    fields = "__all__"
    template_name = 'packsapp/employee/docketDeliveredForm.html'

    def form_valid (self, form):
        product = form.save(commit=False)
        product.save()

        data = form.cleaned_data
        count = 9
        for i in range(1, count):
                ClientInventory.objects.update_or_create(product_short_code=data["product"+str(i)],
                                                 product_quantity=data["product"+str(i)+"_recieved_quantity"],
                                                 product_owner=data['created_for'])

        messages.success(self.request, 'The Delivered Docket was created with success!')
        return redirect('employee:delivered_docket_table')

For your other problem, I would suggest first checking if you have that product and if you do, then modify it instead of creating it again.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this works perfectly but how can i do the second part which is my main problem ?
Just a quick clarification for your second problem, if only the quantity is different, then you want to retrieve the object from the database, update it and then save it. But if anything other than the quantity is different, you want to create a new entry into the database. Right?
Yes, If the product_short_code is same then retrieve the object, sum the quantity.. new quantity+retrieved quantity and save it and if the short code is different then a new entry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.