Issue
I just started in Django, I'm developing a web-application that want to create a Purchase Order
and Masterlist
.
I started using the generic views and now I can create and update a Purchase Order. It has a status
field of Pending
and Received
. If I created a new Purchase Order the default status is Pending
and If I updated it to Received
it should also save the data to Masterlist
table. The thing is, it doesn't work. By the way, the received
item should stay in Purchase Order
table for history.
Since I'm using the generic views in Django.. I tried to put the masterlist
table in the update view of the purchaseorder
but It has error saying No masterlist found matching the query
Here is my class for the UpdateView
class PurchaseOrderUpdateView(LoginRequiredMixin, UpdateView):
model = PurchaseOrder
model = Masterlist
fields = ['item_no', 'description', 'dimension', 'unit', 'quantity', 'cost', 'project_site', 'po_date', 'supplier', 'status']
def form_valid(self, form):
form.instance.prepared_by = self.request.user
return super().form_valid(form)
What I expect is, when I update the status field of my Purchase Order to received, it should also saved the data of it to Masterlist. Or If the item in Purchase Order exists in Masterlist it will update the quantity on it.
Solution
You have to create a get() which receive request parameter. Now in this function get data of the field and check If the status changed to received, Then code to update / save data in Masterlist.
To update / save you can create a common function update_or_create()
Answered By - Rohit Garg
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.