Mike, Clever. The save function seems to write a string of the id. So, self.formulation_no = "50-" + str(self.id) could also work?
Bruckner de Villiers +27 83 625 1086 On 2020/02/12, 00:27, "Mike Dewhirst" <django-users@googlegroups.com on behalf of mi...@dewhirst.com.au> wrote: On 11/02/2020 7:06 pm, Bruckner de Villiers wrote: > > Jason, > > Thank you. I have changed the title of this mail. If I have read the > 10 year old ticket 8576 correctly, it seems to be a DB restriction and > I imagine that there are good reasons for this. However, then I don’t > understand the purpose of the AutoField given the restrictions and > that the Django documentation is somewhat ambiguous/vague. > > The options seem to be: > > * Your uuid suggestion (which I have never worked with, but will > explore) > * a function in the model that does the incrementing before > committing – (anyone have some example code for this?). The > challenge would be to keep track of the last used ticket no. I > haven’t thought this through yet, but it does occur to me that one > could use the id (maybe prefix the id with some starting digits) > and write the record twice. Probably barking up the wrong tree. > I have a need for a unique identifier in one of my models. I save the id to a separate field in the same model. In my case I don't need to see the value. It just needs to be unique and never change. It is used for generating the equivalent of a bar-code in a different model. def save(self, *args, **kwargs): if self.id: if not self.formulation_no: self.formulation_no = str(self.id) super().save(*args, **kwargs) A downside to this is the formulation number stays blank until the next save after creating the record. The upside is that the id isn't going to change so uniqueness isn't lost. Another way of doing this is to copy the id when it is first required and formulation_no is still blank. I have also been known to use a weird mechanism for user-resequencing records in a display. In that case they are lesson sequences within a course and question sequences within a lesson. I use floats and strings in a pair of fields so if that is of interest just ask. Cheers Mike > * > > Regards, > > Bruckner de Villiers > > +27 83 625 1086 > > *From: *<django-users@googlegroups.com> on behalf of Jason > <jjohns98...@gmail.com> > *Reply to: *<django-users@googlegroups.com> > *Date: *Monday, 10 February 2020 at 14:55 > *To: *Django users <django-users@googlegroups.com> > *Subject: *Re: Adding a verbose_name to id field > > Hmm. TIL about https://code.djangoproject.com/ticket/8576 > > Would a a uuid for this, rather than an integer? If not, you'll have > to implement a save override to handle the incrementing yourself. > > In addition, it might be worthwhile bringing this ticket up for > discussion at https://groups.google.com/forum/#!forum/django-developers > > -- > You received this message because you are subscribed to the Google > Groups "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send > an email to django-users+unsubscr...@googlegroups.com > <mailto:django-users+unsubscr...@googlegroups.com>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/dc686528-0a8d-43ff-9973-d478cc765959%40googlegroups.com > <https://groups.google.com/d/msgid/django-users/dc686528-0a8d-43ff-9973-d478cc765959%40googlegroups.com?utm_medium=email&utm_source=footer>. > > -- > You received this message because you are subscribed to the Google > Groups "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send > an email to django-users+unsubscr...@googlegroups.com > <mailto:django-users+unsubscr...@googlegroups.com>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/7C1BE52B-AFAE-4D38-9003-8A454A284A39%40gmail.com > <https://groups.google.com/d/msgid/django-users/7C1BE52B-AFAE-4D38-9003-8A454A284A39%40gmail.com?utm_medium=email&utm_source=footer>. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/99dbcba0-22bd-7c1d-5fe6-68984353bfb1%40dewhirst.com.au. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/A1F957AF-5D4A-488D-AE6B-24CCAD13BDB1%40gmail.com.