Wednesday, May 27, 2020

Re: function model which generate a code for model field

i feel in your last session you are trying to concatenate a string and an integer use the int function to convert it to string then the number will show

On Monday, May 25, 2020 at 5:48:29 AM UTC+14, Anselme SERI wrote:
from django.db import models


def codeid():
lastid = MyModel.objects.last(id)
autocode = ''
if lastid == 0:
autocode = 'BAC000000001'
elif lastid != 0:
if len(lastid) <= 8:
lastid += 1
autocode = 'D/BAC/00000000' + lastid
elif 9 < len(lastid) <= 98:
lastid += 1
autocode = 'D/BAC/0000000' + lastid
elif 99 < len(lastid) <= 998:
lastid += 1
autocode = 'D/BAC/000000' + lastid
return autocode


class MyModel(models.Model):
libelle = models.CharField('Libellé', max_length=100)
code = models.CharField(max_length=30, unique=True, auto_created=codeid())

def __str__(self):
return self.code

Hello,

I try  to to write a correct function for my model which  generate a str + incremental number according last id model.

Pleaz help me.
 

--
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/b6a63f0d-1dbd-4700-b94a-31e6caec7225%40googlegroups.com.

No comments:

Post a Comment