Hi Anselme,
On 27/05/2020 13.33, Anselme SERI wrote:
>
> What I want to do is fill in the 'code' field of my 'Diploma' template.
> For example, the first record of the model will have the value
> 'BAC00001' in the 'code' field, the second 'BAC00002', ...., 'BAC01653'
> .....
> The approach I have chosen to achieve this objective consists in
> defining a function in the model which will retrieve the value of the
> last id of the last record, increment it then concatenate it to the
> chain to obtain the value of the code field of the new recording. code =
> chain + (lastid + 1) with chain = 'BAC000 ..'. I hope it's clear now?
>
There might be better ways to do this, but I would override the save()
method of your model with something like:
def save(self, *args, **kwargs):
self.code = 'BAC{:05}'.format(self.id)
super(MyModel, self).save(*args, **kwargs)
Untested, but hopefully you get the idea.
Kind regards,
Kasper Laudrup
--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ba1a7ce1-0424-4057-e70a-23258a31d166%40stacktrace.dk.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment