I need create a model that uses sequential numbering following this style AA-1-0001. The first 2 Alpha character signify the company code in the military. Currently, this project is just for one company so they don’t need to be adjusted but in terms of future-proofing its a feature I would like. The first numerical number is the last digit of the current Fiscal Year. Followed by the last 4 digits which are the entry number for that FY. Entry Number 221 for Fiscal 2021 would be as follows: AA-1-0221 | Entry Number 222 would be AA-1-0222.
I am new to Django. So I am unsure where to start or how to achieve this. Any assistance is helpful!
Side note: This isn’t really a Django issue - this is a database design issue that would be an appropriate discussion regardless of the application being built.
You’ve got a couple different ways of handling this. First, I’d abandon any idea of using that as a primary key. That opens up your choices.
My first reaction is that I’d probably store that field as three separate components. The company code could be an FK to something like a “CompanyCode” table containing, among other things, the two character code. I might even go so far as to make the second field another FK to a “FiscalYear” table. Finally, I’d create a reference table with FKs to those prior two tables and an auto-increment field for the integer component. Finally, I’d create a function that would return the next-available ID to be inserted into the next row to be created in whatever table you’re using this. That function will need to acquire a row-lock to ensure that concurrent requests don’t return the same value.
Again, this was my first reaction. I’m making a number of assumptions based on a lack of knowledge regarding the larger context of this application. My answer would likely change if any of those assumptions are wrong.
That seems like it would be a good solution just from a beginner stand point. I am very new to this all and it seems VERY complicated at this point in my learning. Especially being that you mentioned its a DB design issue. Basically what I am creating is a website that will allow a HR rep to input the information from a leave form (DA Form 31) into my webpage. Instead of us using a PDF (DA Form 4179) that only holds 14 entries per page. Which is also not searchable if there is an issue with a Service members leave.
I believe at this time, I will get it simple and just got with a CharField thats limited to 9 characters and has helper= that provides an example as to the standard I want.
One of the assumptions I had made was that you’re looking to generate those numbers within the system when the form is being entered. If the numbers are being entered by a person, as opposed to being generated by the system, then yes - your solution will be the easier method.