On Wed, 2009-04-29 at 15:37 -0700, Timboy wrote: > I am trying to make a pk for an object that starts with the year > followed by seven additional digits. YYXXXXXXX I want the digits to > automatically increase like normal. > > I'm guessing I need to pass force_insert=True to my save if not > self.pk. I'd appreciate some advice.
If you're not using Django's AutoField for primary key values (which you aren't), then you need to always supply the primary key value before calling save(). Django cannot guess what value you want to put in there. Now, you could "provide" the value by specifying a (Python) function for the "default" attribute on, say, an integer field and use that to create the value, although there's then no way to enforce the sequential increase if multiple new models are created more or less imsultaneously in separate threads. I would encourage stepping back a bit to consider your problem in a different way, which might be much simpler. Use a normal auto-incrementing sequence for the primary key in the table (i.e. use Django's default) and have another field that specifies the year of creation. Then have a property or method on the model which combines the year-of-creation field with the pk value to get the combined value you're after for display or reference purposes. Modifications on this are possible if you're also doing lookups, etc. Or you could, at the database level, set the sequence value to a particular value before doing the first insert for a year. How this is done is database dependent and is done with direct database modification, not via Django directly (you could do it with cursor.execute(), but it's something you only want to do once a year, in any case). Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---