Hi,
May I get help with this project? I am new to Django. Below is my view and 
model for your review.


class Invoice(models.Model):

    InvoiceNo = ShortUUIDField(length=16,
        prefix="GN_",
        alphabet="1234567890",
        primary_key=True,
        )
    InvoiceDate = models.DateField(default=datetime.now, blank=True)
    Account = models.CharField(max_length=50, blank=True)
    PaymentDueDate = models.DateField(default=datetime.now, blank=True)
    Location  = models.ForeignKey(Location, on_delete=models.DO_NOTHING, 
blank=True, default=1)
   # Product  = models.ForeignKey(Product, on_delete=models.DO_NOTHING)
    Test = models.ManyToManyField("Products", blank=True, 
related_name="products")
    Address   = models.ForeignKey(Address, on_delete=models.DO_NOTHING)
    #Quantity = models.IntegerField(blank=True, default=1)
    Tax = models.IntegerField(default =0)
    SubTotal = models.DecimalField(blank=True, 
null=True,decimal_places=2,max_digits=10)
    Total = models.DecimalField(blank=True, 
null=True,decimal_places=2,max_digits=10)
    #Ful = models.FloatField(blank=True)

    def save(self, *args, **kwargs):
        #rate = Product.objects.get(pk=self.Product.id 
<http://self.product.id/>)
        test = Products.objects.all()
        total = 0
        for my in test:
            total +=  my.SubTotal
        self.SubTotal = total
        self.Total = self.SubTotal + self.Tax
        #self.Total = Sum(self.Total) + Sum( self.SubTotal)
        super(Invoice, self).save(*args, **kwargs)

    # @property
    # def PerformSub(self):
    #      myval = self.Quantity * self.Product.Price
    #      return myval

    # @property
    # def PerformCal(self):
    #     self.Total = self.SubTotal + self.Tax

    # def __str__(self):
    #     return self.InvoiceNo




class Products(models.Model):
    ProductDate = models.DateField(default=datetime.now, blank=True)
    Title = models.CharField(max_length=50, blank=True)
    Description = models.CharField(max_length=150, blank=True)
    Sku = models.CharField(max_length=50, blank=True)
    Price = models.FloatField(blank=True)
    Quantity = models.IntegerField(blank=True, default=1)
    SubTotal = models.DecimalField(blank=True, 
null=True,decimal_places=2,max_digits=10)
    SalesPrice = models.CharField(max_length=50, blank=True)


    def save(self, *args, **kwargs):
       # rate = Products.objects.get(pk=self.Product.id 
<http://self.product.id/>)
        self.SubTotal = self.Price * self.Quantity
        super(Products, self).save(*args, **kwargs)


    def __str__(self):
        return self.Title + "" + self.Description


class InvoiceForm(ModelForm):
    class Meta:
        model = Invoice
        fields = ['InvoiceNo', 'InvoiceDate', 'Account','PaymentDueDate', 
'Location','Test', 'Address', 'Tax', 'SubTotal', 'Total'
        ]



Here is my view. Any help will be much appreciated thank you .





def dashboard(request):

    # forms = admin.get_form(invoices, res)
    location = Location.objects.all()
    address = Address.objects.all()
    products = Products.objects.all()
    form = InvoiceForm()
    # invoice = Invoice.objects.all()
    # invoice = get_object_or_404(Invoice)

    if request.method == 'POST':
        form = InvoiceForm(request.POST)
        if form.is_valid():
            # myinvoice = form.save(commit=False)

            print("test")

            invoiceTest = form.save(commit=True)



            invoice = Invoice()
            invoice.InvoiceNo = form.cleaned_data['InvoiceNo']
            invoice.InvoiceDate = form.cleaned_data['InvoiceDate']
            invoice.Account = form.cleaned_data['Account']
            invoice.PaymentDueDate = form.cleaned_data['PaymentDueDate']
            invoice.Location = form.cleaned_data['Location']
            invoice.Test =  form.cleaned_data['Test']
            invoice.Tax = form.cleaned_data['Tax']
            invoice.SubTotal = form.cleaned_data['SubTotal']
            invoice.Total = form.cleaned_data['Total']




            invoiceTest.save()
            # invoice.save_m2m()

            # print(invoiceForm)




            # invoice = 
Invoice(InvoiceNo=InvoiceNo,InvoiceDate=InvoiceDate,Account=Account,PaymentDueDate=PaymentDueDate,Location=mylocation,Tax=Tax,SubTotal=SubTotal,Total=Total)
            # invoice.save()


            return HttpResponseRedirect('index')


    else:
        form = InvoiceForm()
        # return redirect('dashboard')
    return render(request, 'pages/dashboard.html', { "form" :form , 
'Location': location, 'Address': address, 'Products': products})

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/59e95c89-1d77-4a61-9d1e-bd7846ca3c35n%40googlegroups.com.

Reply via email to