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

194 views
Skip to first unread message

Alexander Joseph

unread,
Jul 25, 2017, 12:23:44 AM7/25/17
to Django users
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

ecas

unread,
Jul 25, 2017, 6:45:12 AM7/25/17
to Django users
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:

Alexander Joseph

unread,
Jul 25, 2017, 1:15:38 PM7/25/17
to Django users
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!

Mike Dewhirst

unread,
Jul 25, 2017, 6:48:22 PM7/25/17
to django...@googlegroups.com
On 26/07/2017 3:15 AM, Alexander Joseph wrote:
> How did you learn all the methods that are available

Django is very comprehensively documented and almost any question you
can think up is only a google search away. Typically a search will
reveal a Django doc reference and StackOverflow answers. I must say
looking up the docs is very valuable because you learn so much else
besides your original quest.

Books are another good source. My preferred one is Two Scoops of Django.

Welcome.


Alexander Joseph

unread,
Jul 26, 2017, 7:18:26 PM7/26/17
to Django users
Thanks I'll pick up that book.

Would another acceptable approach be to do this in the view by overriding the form_valid() method? Can you tell me if there is any benefit/disadvantage to doing it one way or another? Thanks again

Mike Dewhirst

unread,
Jul 26, 2017, 8:42:05 PM7/26/17
to django...@googlegroups.com
On 27/07/2017 9:18 AM, Alexander Joseph wrote:
> Thanks I'll pick up that book.
>
> Would another acceptable approach be to do this in the view by
> overriding the form_valid() method? Can you tell me if there is any
> benefit/disadvantage to doing it one way or another? Thanks again

If you can do it in the model that is where it should be done.

I could be wrong but I believe fundamental business rules should be
built into the model as well.

So I override the model.save() method to do the auto-fill and override
the model.clean() method for business rules. The model clean() method
gets called by form.is_valid() automatically on saving in the browser
and can raise an exception if for example some perfectly valid data
violates a business rule.

https://docs.djangoproject.com/en/1.10/ref/models/instances/#validating-objects

Consider that down the track you wish to make a phone app with access to
your database. The only reasonable way to do that is via the model API.
That means you have to do the work in your phone app that you did in
your web form. It is likely easier to just call the model.full_clean()
method before model.save() in the API.

>
>
>
>
>
> On Tuesday, July 25, 2017 at 4:48:22 PM UTC-6, Mike Dewhirst wrote:
>
> On 26/07/2017 3:15 AM, Alexander Joseph wrote:
> > How did you learn all the methods that are available
>
> Django is very comprehensively documented and almost any question you
> can think up is only a google search away. Typically a search will
> reveal a Django doc reference and StackOverflow answers. I must say
> looking up the docs is very valuable because you learn so much else
> besides your original quest.
>
> Books are another good source. My preferred one is Two Scoops of
> Django.
>
> Welcome.
>
>
> --
> 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...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@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/5c76493d-11a4-4460-9522-adc2822765e9%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/5c76493d-11a4-4460-9522-adc2822765e9%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Alexander Joseph

unread,
Jul 26, 2017, 9:56:17 PM7/26/17
to Django users
Good to know. Thanks again!
Reply all
Reply to author
Forward
0 new messages