Thanks Guys, Will, your snippet was exactly what I was looking for - thanks for that. Alas Rama, the client has specified that these custom Invoice IDs get saved in the db alongside the primary key, otherwise I'd go with your suggestion (simpler!)
On Feb 5, 8:27 am, Rama Vadakattu <ramaakrish...@gmail.com> wrote: > CUSTOM_ID-----------------------ID > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > INV00001 ---------------------- 1 > INV00002 -----------------------2 > INV00003------------------------3 > . > . > . > i think you need INV00001 for display purposes and also i hope it does > not matter much whether you store 1 or INV00001 in the db. > > in such cases why can't you follow the below approach? > ------------------------------------------------------ > store as 1 but display it as INV00001 (by appending 1 to 'INV0000') > when you want to search 'INV00002' strip out the INV and convert the > other part to 2 and search for this 2 in the database. > > ~~ > I don't know whether this is feasible to you or not but just a thought > adding to suggestion of "Will Hardy". > > On Feb 3, 4:47 am, Will Hardy <e.willha...@gmail.com> wrote: > > > There's no easy solution without saving the object to the database. > > Auto incrementing fields (AutoField or just id) get their value from > > the database, so they wont have one until you save. This is good > > because it prevents multiple objects ever having the same value. One > > way to do what you want is to save a new invoice to the database > > straight away when the user clicks "add new invoice" and allow them to > > edit it, but if you're using the admin site, this might not be > > straightforward. > > > You wont need to create a new field by the way, you could simply make > > a property that uses the ID field of a model: > > > @property > > def invoice_id(self): > > if self.id: > > return 'INV%d' % self.id > > > But you wouldn't be able to search for the full INV0000001 string, you > > would have to strip the INV beforehand or create a new charfield and > > populate that on save (sounds like what you're doing) > > > If you don't want to have such obvious incrementing values for your > > invoice numbers, you could use a perfect hash function to convert it > > to a more obscure value, > > likehttp://www.djangosnippets.org/snippets/1249/(Iwrote this snippet, > > don't worry that two people voted against it, they didn't say why... I > > have no idea... it's just a simple perfect hash function and base > > converter, and it certainly does the job it was designed to do) > > > Cheers, > > > Will --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---