I have these two models, Invoice
representing an invoice e Item
that represents the items on the invoice.
from django.db import models
# Create your models here.
class Invoice(models.Model):
vendor = models.CharField(max_length=200)
client = models.CharField(max_length=200)
number = models.CharField(max_length=200)
date = models.DateTimeField(max_length=200)
due_date = models.DateTimeField()
def __str__(self):
return "Invoice number: {}".format(self.number)
class Item(models.Model):
invoice = models.ForeignKey(Invoice, on_delete=models.CASCADE)
description = models.TextField()
quantity = models.DecimalField(max_digits=19, decimal_places=2)
rate = models.DecimalField(max_digits=19, decimal_places=2)
amount = models.DecimalField(max_digits=19, decimal_places=2)
subtotal = models.DecimalField(max_digits=19, decimal_places=2)
tax = models.DecimalField(max_digits=19, decimal_places=2)
notes = models.TextField()
terms = models.TextField()
def __str__(self):
return "{}".format(self.description)
And these are the forms where the information will be collected:
from django import forms
from .models import Invoice, Item
class InvoiceForm(forms.ModelForm):
class Meta:
model = Invoice
fields = ('vendor','client', 'number', 'date', 'due_date')
widgets = {
'date': forms.TextInput(attrs={'class':'datepicker'}),
'due_date': forms.TextInput(attrs={'class':'datepicker'}),
}
class ItemForm(forms.ModelForm):
class Meta:
model = Item
fields = ('description', 'quantity', 'rate', 'amount',
'subtotal', 'tax', 'notes', 'terms')
My question is how do I refer to any of the ItemForm
fields in the template to capture the data?
Because at least with InvoiceForm
I have no problems, he shows them to me. For example, if I do this, I do not have any inconveniences:
<!-- Client -->
<div class="row">
<div class="input-field col s4">
<label for="{{ form.client.id_for_label }}">Client</label>
{{ form.client }}
</div>
<div class="input-field col s4 offset-s4">
<label for="{{ form.due_date.id_for_label }}">Due Date</label>
{{ form.due_date }}
</div>
</div>
But when trying to make reference to some field of ItemForm
I do not know how it should be done. For example if I want in the template to show the field description
of ItemForm
as I should do? Because if I try something like that, it does not show me anything:
<div class="input-field col s5">
{{ form.description }}
</div>
This is my view, although I do not know how to connect both forms, for now I'm only interested in them unless they are displayed correctly in the template:
def invoice_generator(request):
form = InvoiceForm
return render(request, 'invoiceapp/invoice_generator.html', {'form': form})
Maybe I'm badly focused or I'm getting complicated, but I really do not know how to proceed.
I appreciate your help.