class Invoice(models.Model): created_at = models.DateTimeField(auto_now_add=True) their_company = models.ForeignKey(Contact, on_delete=models.CASCADE,blank=True, null=True,) invoice_number = models.CharField(max_length=50, default='') bill_to = models.CharField(max_length=255, default='') in_trash = models.BooleanField(default=False) created_by = models.ForeignKey(settings.AUTH_USER_MODEL) def save(self, *args, **kwargs): if not self.id: today = datetime.date.today() date_str = datetime.datetime.strftime(today, '%y%m%d') doc_str = 'IN' last_invoice = Invoice.objects.filter(invoice_number__startswith=doc_str).order_by('invoice_number').last() if last_invoice: last_invoice_num = last_invoice.invoice_number[-2:] new_invoice_num = int(last_invoice_num) + 1 new_invoice_num = "%02d" % new_invoice_num else: new_invoice_num = '01' self.invoice_number = doc_str + date_str + new_invoice_num super(Invoice, self).save(*args, **kwargs) class Meta: ordering = ['invoice_number', ] def __str__(self): return self.invoice_number def get_absolute_url(self):
class InvoiceCreateView(LoginRequiredMixin, CreateView): fields = ("their_company", "bill_to") model = Invoice form = forms.CreateInvoiceForm() def form_valid(self, form): object = form.save(commit=False) object.created_by = self.request.user object.save() document = Document() #use invoice data to create invoice #save the doc document.save('invoice.docx') return super(InvoiceCreateView, self).form_valid(form)
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e04ce903-156b-44bc-af39-9672ffbf9d9e%40googlegroups.com.--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/Tn00OT3b75E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.