if what you are doing is a quote, you don't need to get the total from
the user. I would go one step further, don't get price from the user.
Form can be altered.

I would do the model like that:


    class Invoice(models.Model):
        user = models.ForeignKey(User, models.CASCADE)
        date = models.DateTimeField()


    class InvoiceItem(models.Model):
        invoice = models.ForeignKey(Invoice, models.CASCADE,
related_name="items")
        product = models.ForeignKey(Product, models.CASCADE)
        quantity = models.PositiveSmallIntegerField()

        def total(self):
            return product.price * quantity


With this setup, if `invoice` is an Invoice object, invoice.items is
the list of product related to the invoice. In you JS, you will have 3
HTML elements of interest. On represent either the price or the item,
the second the quantity, and the third the total. If either of the two
first one change, update the total.

    <select id=itm name=item>
        <choice value=1 data-value=100>Pokeball</choice>
        <choice value=2 data-value=500>Greatball</choice>
    </select>
    <input type=number id=qty name=quantity min=0 max=99>
    <input id=total>
    <script>
        $('itm').on('change', updateTotal)
        $('qty').on('change', updateTotal)

        function updateTotal() {
            price = parseInt( $('itm').get_attribute('data-value') )
            quantity = parseInt( $('qty').value() )
            $('total').update('$'+price*quantity)
        }
    </script>

(The script don't work as is but should give the general idea)
This way, the user see instantly the effect of their change but your
application only work with safe value.

I hope I didn't go too far off-path and that will be useful to you :)

2016-08-13 22:12 GMT+02:00 Paul Kudla <paul.ku...@gmail.com>:
> I am running django 1.10 and assume i will need to use java scripts, jquery
> or angularjs
>
> Simply put i have a decimal field that i want to add to another decimal
> field and place the answer in a third decimal field.
>
> I want this to be displayed on the fly as the form gets data inputed on the
> client side into it (aka no submit and display the answer immediately for
> example)
>
> And then update the relative fields upon a save request (the data would
> already be in the form)
>
> This is a very basic function but is nessesary on any invoice program etc ie
> to add up the actual total of the invoice for example or multiply a qty of
> one against the amount of the item and then total it, for example in an
> invoice body using tabularinline.
>
> There must be examples on how to modify the admin form template to allow for
> this, I have just been unable to find any documentation that shows how to do
> this in a simple way.
>
>
>
> Any help would be appreciated.
>
> basic model :
>
> #models.py (Remote Access VNC Application)
> from django.db import models
>
>
> class Test_Add(models.Model):
>         siteid       = models.CharField(max_length=64, primary_key=True,
> unique=True)
>         field1           = models.DecimalField(verbose_name='field1',
> max_digits=10, decimal_places=2, null=True, blank=True)
>         field2           = models.DecimalField(verbose_name='field2',
> max_digits=10, decimal_places=2, null=True, blank=True)
>         field3           = models.DecimalField(verbose_name='field3',
> max_digits=10, decimal_places=2, null=True, blank=True)
>         field4           = models.DecimalField(verbose_name='field4',
> max_digits=10, decimal_places=2, null=True, blank=True)
>
>
>         class Meta:
>                 ordering = ['siteid',]
>                 db_table = u'test_add'
>                 verbose_name = u"Test template"
>                 verbose_name_plural = u"Test Template"
>
>         def __unicode__(self):
>                 return self.siteid
>
> Basic Admin :
>
> #admin.py - (Remote Access VNC Application)
> from django.contrib import admin
>
> #import the application stuff
> from Add.models import Test_Add
>
>
> class Test_Add_Admin(admin.ModelAdmin):
>
>         fieldsets = [
>                 ('Site Information',    {'fields': ['siteid',
> 'field1','field2','field3','field4']}),
>
>         ]
>         list_display = ('siteid', 'field1', 'field2', 'field3','field4',)
>         list_display_links = ('siteid', )
>
>         search_fields = ['siteid', ]
>
>
> admin.site.register(Test_Add, Test_Add_Admin)
>
>
> --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/8303e4b8-b02f-49be-b48e-b68b87fdc4a1%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



-- 

Cordialement, Coues Ludovic
+336 148 743 42

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAEuG%2BTYbxvKjohrUC8skgw8BBPAUbt9szg--Xn7dJYQAq0fp%3Dg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to