Tuesday, July 25, 2017

Re: How can I auto-fill a field in the model without showing a form field to the user?

Thanks very much, that works great!

I'm still lost on what all the methods are that I am able to override. How did you learn all the methods that are available, like the save method, to override? I used some functions in the past with php but mostly I did procedural programming and used a low level MVC framework called CodeIgniter which was very simple to learn and start using. I'm having a harder time with django though, mostly because it seems to me like there is a lot of magic happening behind the scenes - like this save method

Thanks again for your help!





On Tuesday, July 25, 2017 at 4:45:12 AM UTC-6, ecas wrote:
One way could be to overload the save method of the model. Maybe something like this in your models.py:

import datetime

class Invoice(models.Model):
    ....
    def save(self, *args, **kwargs):
        if not self.id:
            today = datetime.date.today()
            date_str = datetime.datetime.strftime(today, '%y%m%d')
            last_invoice = Invoice.objects.filter(invoice_number__startswith=date_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 = date_str + new_invoice_num
        super(Invoice, self).save(*args, **kwargs)


El dimarts, 25 juliol de 2017 6:23:44 UTC+2, Alexander Joseph va escriure:
I'm new to django, but coming from php I think its the greatest thing ever.

I have a model for Invoices ...

{{{
from django.conf import settings
from django.db import models
from django.core.urlresolvers import reverse
#from django.contrib.auth.models import User

# Create your models here.
class Invoice(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    their_company = models.CharField(max_length=255)
    invoice_number = models.CharField(max_length=50, default='')
    bill_to = models.CharField(max_length=255, default='')
    created_by = models.ForeignKey(settings.AUTH_USER_MODEL)
    
    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})
}}}

right now the user has to put in the invoice number themselves. I want to not give the user that option though - I'd like to autofill that field in the database with a concatenation of the 2 digit year (17) plus 2 digit month (07) plus 2 digit day (24) and then a 2 digit auto increment for every invoice created on that specific date so the first invoice created today would be 17072401 and the second would be 17072402, etc. I'm guessing I need a field just for the date and another for the iteration of each invoice.

Can anyone give me some direction on this? First I'm not sure how to autofill a field in the database without getting the input from the user, and I'm also not sure how I would do the concatenation with the right numbers. Thanks for your help

--
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+unsubscribe@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/e99393cf-0764-45e4-9fde-5e12dbf79bf6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment