Wednesday, May 27, 2020

Re: function model which generate a code for model field


Hello,

I will try to explain it to you in the language I understand best and I hope that you will have an excellent translator to identify me.

english version
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?

french version
Ce que je souhaite faire, c'est remplir le champ 'code' de mon modele 'Diplôme'. Par exemple le premier enregistrement du modèle aura la valeur 'BAC00001' dans le champ 'code', le deuxième 'BAC00002',...., 'BAC01653'.... .
L'approche que j'ai retenue pour atteindre ce objectif consiste à   définir une fonction dans le modèle qui va récupérer la valeur du dernier id du dernier enregistrerment, l'incrémenter puis la concatener à la chaine pour obtenir la valeur du champ code du nouvelle enregistrement. code = chaine + (lastid + 1) avec chaine = 'BAC000..'. J'espère que c'est clair maintenant?

Best regards

Anselme S.



Le mer. 27 mai 2020 à 06:53, Derek <gamesbook@gmail.com> a écrit :
Its not clear what you mean by "last"; do you mean a record that already exists in the database and has a ID value - or do you want the ID of the current record?  Either way, the code field should not be resetting the '0' to a '1' as this is then misrepresenting the actual data.

PS The logic to create the string looks a bit clunky; why not use rjust to get the correct number of prefix zeros?

On Monday, 25 May 2020 22:33:30 UTC+2, Anselme SERI wrote:
hello,
I am trying to create a function which will recover the last id of my model which I will increment and add to a character string, and I would like to add this new value (x = last id incremented + string) be added to the "code" field "of my model when I make a new recording.
How else could we use the id field which automatically increments to perform such an action?

Anselme S.

Le dim. 24 mai 2020 à 20:12, Kasper Laudrup <lau...@stacktrace.dk> a écrit :
Hi Anselme,

On 24/05/2020 17.48, Anselme SERI wrote:
>
> I try  to to write a correct function for my model which  generate a str
> + incremental number according last id model.
>

Your code is obviously wrong for quite a few reasons, but I at least
simply cannot understand what you are trying to achieve.

What should the "correct function" do?

The id field of a model is already incremental. Do you simply want to
append that to a string and what do you want to do with that string?

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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/65ffe493-1190-619a-f904-fbe35b60e3e3%40stacktrace.dk.

--
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/f8725fd3-bf12-4b82-b1d6-e8569d7220ff%40googlegroups.com.

--
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/CACbA33o_2tQoCXA%2B8iPYE1hFK1SExj%3DQmywp1nsxaV0NX9p9Bg%40mail.gmail.com.

No comments:

Post a Comment