Wednesday, July 26, 2017

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

Good to know. Thanks again!

On Monday, July 24, 2017 at 10:23:44 PM UTC-6, Alexander Joseph wrote:
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/7d5793b5-f760-4522-92eb-69f1cbb1c5d9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment