Hello everyone,
I have created the following model in my Project.
User = settings.AUTH_USER_MODEL
Discount_Type=(
('$', '$'),
('%', '%'),
)
*class Invoice(models.Model):*
ticket = models.OneToOneField(Ticket, related_name='invoice',
blank=True,null=True, on_delete=models.CASCADE)
updated_by = models.ForeignKey(User, null=True, blank=True,
related_name='invoice_editor', on_delete=models.SET_NULL)
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
is_paid = models.BooleanField(default=False)
federal_price = models.DecimalField(default=20.00, max_digits=6,
decimal_places=2)
federal_qty = models.PositiveIntegerField(default=1)
federal_amount = models.DecimalField(null=True, blank=True, max_digits=6,
decimal_places=2)
state_price = models.DecimalField(default=20.00, max_digits=6,
decimal_places=2)
state_qty = models.PositiveIntegerField(default=1)
state_amount = models.DecimalField(null=True, blank=True, max_digits=6,
decimal_places=2)
discount = models.DecimalField(default=0.00, max_digits=6, decimal_places=2)
discount_type = models.CharField(default='$', max_length=5,
choices=Discount_Type)
discount_value = models.DecimalField(null=True, blank=True, max_digits=6,
decimal_places=2)
amount_payable = models.DecimalField(null=True, blank=True, max_digits=6,
decimal_places=2)
def __str__(self):
return str(self.amount_payable)
class Meta:
ordering = ['-date_updated']
*class Ticket(models.Model):*
tax_year = models.CharField(max_length=50, choices=Tax_Years,
default='2020')
service = models.CharField(max_length=50, choices=Services,
default='Tax Filing')
description = models.TextField(max_length=1000, blank=True)
# Customer
customer = models.ForeignKey(User, related_name='ticket_customer',
on_delete=models.CASCADE)
My forms.py
*class TicketCreateForm(forms.ModelForm):*
class Meta:
model = Ticket
fields = ('tax_year', 'service', 'description',)
widgets = {
'tax_year': forms.Select(attrs={'class': 'form-control'}),
'service': forms.Select(attrs={'class': 'form-control'}),
'description': forms.Textarea(attrs={'rows':3, 'cols':60,
'class':'form-control'}),
}
My *Views.py*
*def customer_ticket_create(request):*
form = TicketCreateForm()
if request.method == 'POST':
form = TicketCreateForm(request.POST or None)
if form.is_valid():
ticket = form.save(commit=False)
ticket.customer = request.user
ticket.save()
Invoice.objects.create( ticket=ticket, updated_by=request.user)
*return *redirect('customer_home')
But I'm getting *django.db.utils.IntegrityError: "user_id" violates
not-null constraint.*
*This error is because of * Invoice.objects.create(ticket=ticket,
updated_by=request.user)
Without this line, my view is working fine. Not sure why the view is not
able to create Invoice. Can someone please help me.
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/6a04a497-721c-42dc-8649-137c6b5a56f3n%40googlegroups.com.