Hello, I have an app for creating invoices, but I'd like to be able to save 
a .docx of the invoice when the user creates the invoice (I'm using docx 
library). I'm very new to django so any direction you can give I 
appreciate. I'd like to know the proper way to go about doing this.

Right now I have it so that the 'invoice_number' is generated in the model 
in the save method and the 'created_by' user is assigned to the 
self.request.user in the view. Please let me know if there is a better way 
to do this also. Thanks for your help!

Here is my model ...


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):
        return reverse("accounting:invoices:detail", kwargs={"pk": self.pk})




and here is my InvoiceCreateView ...


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)



-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e49d123c-db15-49ff-baa8-996ee5fd2807%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to