Thanks Mike - I have just become cleverer. Bruckner de Villiers 083 625 1086
On 2020/02/12, 09:55, "Mike Dewhirst" <[email protected] on behalf of [email protected]> wrote: On 12/02/2020 6:11 pm, Bruckner de Villiers wrote: > Mike, > Clever. The save function seems to write a string of the id. So, self.formulation_no = "50-" + str(self.id) could also work? Fact is you can do anything you like. Instead of what I showed you could call any method you care to write either before or after the super() call in model.save() You can also use pre_save and post_save signals to do much the same thing. Where I need to do such things my preference is a model save method which looks like this ... def save(self, *args, **kwargs): self._pre_save() # stuff to be computed pre-save super().save(*args, **kwargs) self._post_save() # stuff to be done after the save Specifically, yes my requirement was to have a models.CharField so str(self.id) was the way to satisfy that. Cheers Mike > > Bruckner de Villiers > +27 83 625 1086 > > On 2020/02/12, 00:27, "Mike Dewhirst" <[email protected] on behalf of [email protected]> 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: *<[email protected]> on behalf of Jason > > <[email protected]> > > *Reply to: *<[email protected]> > > *Date: *Monday, 10 February 2020 at 14:55 > > *To: *Django users <[email protected]> > > *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 [email protected] > > <mailto:[email protected]>. > > 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 [email protected] > > <mailto:[email protected]>. > > 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 [email protected]. > 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/97c794ab-468f-7733-5ca2-bd86d5ec7998%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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/F0882A84-368E-4A46-A37F-F1FE40237A73%40gmail.com.

