Tuesday, July 1, 2014

Re: how to saven models dynamically inputted by user in form

Hi Aashita,

There's a couple of things happening here. I'm no expert and this is a
rushed end of evening email.

For getting the obj.save() to work, here:

cd = form.cleaned_data
newPI = PurchasedItem()
newPI.name = cd['quote_item']
newPI.qty = cd['quote_qty']
newPI.save()

You now have a new PurchaseItem with a name and a qty.

Having said that, is there a reason you aren't using ModelForms and
Generic Views?

Then there's the PurchaseOrder. Will there be other PuchaseItems on
the order? Is it ManyToMany?

If it is, it might be better to have the PurchaseOrder Form, with
PurchaseItem as an attached ModelFormset - in that way you can have as
many PIs on you PO as you want.

The easiest way to get the sum price on a PO then, are a small fn on
the PO model:

def total(self):
total_cost = 0
for pi in self.pi_set.all():
total_cost += pi.price
return total_price

or something like that. Using aggregates is a good idea, but I'm not
fully across the syntax.

Then your last render can look like:

return render(request, 'bills/bills.html', {'purchase_order' : purchase_order})

You can call {{ purchase_order.total }} in the template, and there is
no need to pass the form at that point?



Good luck

Cheers
L.





On 1 July 2014 21:32, Aashita Dutta <aashitadutta@gmail.com> wrote:
> this is my code-
> def final(request, name):
> if request.method == 'POST':
> form = ConfirmForm(request.POST)
> if form.is_valid():
> cd = form.cleaned_data
> quote_item = request.POST['quote_item']
> quote_qty = request.POST['quote_qty']
> #obj = PurchasedItem.objects.create(item__name=quote_item,
> qty=quote_qty,price=quote_price)
> obj = PurchasedItem(item__name=quote_item__name, qty=quote_qty)
> obj.save()
> purchase_order = PurchaseOrder.objects.get(pk=name)
> item =
> PurchasedItem.objects.filter(purchase_order_id=name).values_list('item__name',
> 'qty', 'price')
> total_cost =
> PurchasedItem.objects.filter(purchase_order_id=name).aggregate(Sum('price')).get('price__sum',
> 0.00)
> return render(request, 'bills/bills.html', {'purchase_order' :
> purchase_order,
> 'item' : item, 'total_cost' : total_cost, 'form':form })
>
> else:
> form = ConfirmForm()
> return render(request,'bills/bills.html')
>
>
>
> in this code obj.save() method is not working . Kindly help.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscribe@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/c2c02b8c-0b14-4072-a97c-a4e452504afb%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
The idea is that a beautiful image is frameable. Everything you need
to see is there: It's everything you want, and it's very pleasing
because there's no extra information that you don't get to see.
Everything's in a nice package for you. But sublime art is
unframeable: It's an image or idea that implies that there's a bigger
image or idea that you can't see: You're only getting to look at a
fraction of it, and in that way it's both beautiful and scary, because
it's reminding you that there's more that you don't have access to.
It's now sort of left the piece itself and it's become your own
invention, so it's personal as well as being scary as well as being
beautiful, which is what I really like about art like that.
-----------------------------------------------------------------------------------------------------------
Adventure Time http://theholenearthecenteroftheworld.com/

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGBeqiOP9FKigUwOLF8_%3Dk8JxcqAj5SNuMna1d%3D7c-j-dqU9qA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment