我正在尝试创建销售发票表格,在该表格中我使用的是model和modelformset,但是它给出了以下错误: NOT NULL约束失败:saleinvoice_saletransiction.transiction_date
我不确定问题出在哪里
def testCreate(request):
form = Sale_Invoice_Main_Page
modelformsets = Sale_Transction_formset
if request.method == 'POST':
forms = form(request.POST)
formset = modelformsets(request.POST)
if forms.is_valid() :
forms.save(commit=True) and formset.save(commit=True)
date = forms.invoice_date
invoice = forms.invoice_no
inv = Invoice_max.objects.get(invoice_name='sale')
inv.invoice_no = invoice + 1
inv.save()
transiction_type = "sale"
for set in formset:
set.transiction_date = date
set.invoice_no = invoice + 2
set.transiction_type == transiction_type
set.author = request.user.username
forms.author = request.user.username
forms.save()
formset.save()
return HttpResponseRedirect(reverse_lazy('saleinvoice:sale invoice home page'))
else:
forms = form(initial= { 'invoice_no' : sale_invoice.invoice_no + 8 ,
'invoice_date': datetime.now()})
formset = modelformsets()
return render(request, 'saleinvoice/test for model factory.html',{ "saleform" : forms,
"saletransiction" : formset})
class Sale_Invoice_Main_Page(forms.ModelForm):
class Meta:
model = SaleInvoice
fields = '__all__'
exclude = ['created_at','updated_at','transiction_type','author']
widgets = {'description' : forms.TextInput(attrs={ 'placeholder' : 'description'}),
'invoice_no' : forms.TextInput(attrs={ 'readonly' : 'True'}),
'total_amount' : forms.TextInput(attrs={ 'readonly' : 'True'}),
'invoice_date' : forms.DateInput(attrs={ 'class' : "vdate" }),
'due_date' : forms.DateInput(attrs={ 'readonly' : "True" }),
}
Sale_Transction_formset = modelformset_factory(SaleTransiction,
fields=( 'description', 'price','quantity','total','item'),
exclude= ('author','invoice_no','transiction_type','transiction_date'),
extra=1,
widgets={ 'description' : forms.TextInput(attrs={ 'placeholder' : 'optionall','class' : 'description'}),
'price' : forms.NumberInput(attrs={ 'readonly' : 'True', 'class' : 'rate'}),
'total' : forms.NumberInput(attrs={ 'readonly' : 'True', 'class' : 'total'}),
'quantity' : forms.NumberInput(attrs={'class' : 'quantity'}),
'item' : forms.Select(choices=Customer_data.objects.none().values_list('id','customer_Name'),attrs={ 'class' : "item_list"}),
},
)
class SaleInvoice(models.Model):
customer = models.ForeignKey(Customer_data , on_delete=models.CASCADE)
invoice_date = models.DateField(null=True,blank=True)
invoice_no = models.PositiveIntegerField(unique=True)
due_date = models.DateField(blank=True,null=True)
address = models.TextField()
total_amount = models.PositiveIntegerField(null=True,blank=True)
description = models.TextField(null=True,blank=True)
transiction_type = models.CharField(max_length=50,blank=True)
author = models.CharField(max_length=30)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.address
class SaleTransiction(models.Model):
invoice_no = models.PositiveIntegerField(unique=True,null=True,blank=True)
customer_transiction_name = models.CharField(max_length=30)
transiction_date = models.DateField()
item = models.CharField(max_length=100)
description = models.TextField(null=True,blank=True)
price = models.PositiveIntegerField()
quantity = models.PositiveIntegerField()
total = models.PositiveIntegerField()
transiction_type = models.CharField(max_length=50,blank=True)
author = models.CharField(max_length=30)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.item
<form method="post">
{% csrf_token %}
<div style="display: inline">
{{ saleform.customer.label}}
{{ saleform.customer }}{{ saleform.invoice_date.label }}{{ saleform.invoice_date}}{{ saleform.due_date.label }}
{{ saleform.due_date }}<br>{{ saleform.invoice_no.label }}{{ saleform.invoice_no }}
</div><br>
{{ saleform.address.label }}<br>
{{ saleform.address }}<br>
{{ saleform.description }}<br>
{{ saleform.errors }}
{{ saletransiction.errors }}
<div class="w3-row w3-padding-16">
<table class="w3-table w3-border w3-striped" >
<tr >
<th >Item</th>
<th>Description</th>
<th>Price</th>
<th>Quantity</th>
<th>Amount</th>
<th>Delete</th>
</tr >
{{ saletransiction.management_form }}
<div id="addrow">
{% for sale in saletransiction %}
<tr class="forrow " >
<td>{{ sale.item}}</td>
<td>{{ sale.description }}</td>
<td>{{ sale.price }}</td>
<td>{{ sale.quantity }}</td>
<td>{{ sale.total }}</td>
<td><button class="removerow">Remove</button> </td>
</tr>
{% endfor %}
</div>
</table>
</div>
{{ saleform.total_amount.label }}
{{ saleform.total_amount }}<br>
<input type="submit">
</form>
我想找到我在代码中做错的地方,我认为我在做错事,但是我找不到错误的地方