How do I add a Custom ID Field that increments?

9 views
Skip to first unread message

Alfonso

unread,
Feb 2, 2009, 2:42:25 PM2/2/09
to Django users
Hey,

I want to add a field to an invoice model that contains a custom auto-
incrementing value in the format - 'INV000001" which increments...
INV000002 (obviously!). Anyone have a simple way I can build that
into the model when a user clicks 'Add New Invoice'? I've written
custom save methods but not one that implements on adding a new
record?

Thanks, any help would be great


Will Hardy

unread,
Feb 2, 2009, 6:47:29 PM2/2/09
to django...@googlegroups.com
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, like
http://www.djangosnippets.org/snippets/1249/ (I wrote 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

Rama Vadakattu

unread,
Feb 5, 2009, 3:27:17 AM2/5/09
to Django users
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/(I wrote this snippet,

Alfonso

unread,
Feb 9, 2009, 7:50:37 AM2/9/09
to Django users
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!)
> > to a more obscure value, likehttp://www.djangosnippets.org/snippets/1249/(Iwrote this snippet,
Reply all
Reply to author
Forward
0 new messages