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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---