Django inlineformset autocomplete and autopopulate by ajax

256 views
Skip to first unread message

Ajay Kumar

unread,
Feb 12, 2015, 6:34:10 AM2/12/15
to django...@googlegroups.com

I'm trying to do this following link feature in django,

Here by choosing country the rest of the fields will be auto populated, like this i'm trying to achieve in django, If any idea pls tell me it will be very great for me


This is Product table,by this i will add many products
models.py
class Product(models.Model):
     store
= models.ForeignKey(Store)
     code
= models.CharField(verbose_name='Product Code', max_length=128, blank=False)
     name
= models.CharField(verbose_name='Product Name', max_length=128, blank=False)
     unit_price
= models.FloatField(verbose_name='Unit Price', blank=False)
     in_stock
= models.IntegerField(verbose_name='In Stock', default=0, blank=False)


This is Invoice order table, in this i will get customer details
class Invoice(models.Model):
     customer
= models.ForeignKey(Customer)
     date
= models.DateTimeField(auto_now=True, blank=False)


This in Invoice Item order table which is in inline formset 
class InvoiceItem(models.Model):
     invoice
= models.ForeignKey(Invoice)
     product
= models.ForeignKey(Product)
     particular
= models.CharField(verbose_name='Particular', max_length=128, blank=False)
     quantity
= models.IntegerField(verbose_name='Quantity', default=0)
     unit_price
= models.FloatField(verbose_name='Unit Price', blank=False)
     
tax_rate_percentage = models.FloatField(verbose_name='Tax Rate Percentage', blank=False)

views.py
I'm trying to raise invoice by autopopulate feature.
def add_invoice(request):
  store
= Store.objects.get(id=pk)
  product
= Product.objects.filter(store=store)
  context
=  RequestContext(request)
 
InvoiceItemInlineFormSet = inlineformset_factory(Invoice, InvoiceItem, extra = 1,exclude=('invoice',))
 
if request.method == 'POST':
    invoiceform
= InvoiceForm(request.POST)
    invoiceformset
=  InvoiceItemInlineFormSet(request.POST)
   
if invoiceform.is_valid() and invoiceformset.is_valid():
       invoice
= invoiceform.save(commit=False)
       invoice
.store = store
       invoice
.save()
       invoiceformset
.save(commit=False)
       invoiceformset
.instance = invoice
       invoiceformset
.save()
       
return HttpResponseRedirect('/store_invoice/%s/invoice/'% pk)
   
else:
      invoiceform
= InvoiceForm()
      invoiceformset
=  InvoiceItemInlineFormSet()
   args
= {}
   args
.update(csrf(request))
  args
= {'email':email,'store':store,'invoiceform':invoiceform, 'invoiceformset':invoiceformset}
 
return render_to_response('invoice/invoice_add.html',args,context)


invoice_add.html

{% extends "base_site.html" %}
{% load staticfiles %}
{% block title %}Add New Order | Finvoicez{% endblock %}
{% block page-heading %}Add New Order{% endblock %}
{% block content %}
<script src="{{ STATIC_URL }}js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
        $
(function() {
            $
(".inline.{{ invoiceformset.prefix }}").formset({
                prefix
: "{{ invoiceformset.prefix }}",
           
})
       
})
   
</script>
<div class="the-box no-border">
    <form action="/
store_invoice/{{store.id}}/invoice/add/"  method="post">{% csrf_token %}
        <div class="
row">
            <div class="
col-sm-4">
                <label>Customer</label>
                {{invoiceform}}
            </div>
        </div>
        <fieldset>
            <legend>Order Items</legend>
            {{ invoiceformset.management_form }}
            {{ invoiceformset.non_form_errors }}
            {% for form in invoiceformset %}
                {{ form.id }}
                <div class="
inline {{ invoiceformset.prefix }}">
                    {{form}}
                </div>
            {% endfor %}
        </fieldset>
        <button type="
submit" class="btn btn-success">Add Order</button>
    </form>
</div>
{% endblock %}


Here i'm trying to bring when i'm adding Invoice Item , by selecting the product code the rest of fields want to be autopopulate by corresponding Product details(Particular,unit price and tax) and it must be in inlineformset, for every 'add another' it must be autopopulate, like above reference link.
thanks in advance


Reply all
Reply to author
Forward
0 new messages