0

(first of all sorry for my bad english, i hope you can understand me)

Well i have to add a Custom ID to my objects of one of my models, this custom id is going to have a letter P or L if the related object have one or other attribute. Specifically if the employee has the "Planta Permantente" or "Planta Contratada" attribute the custom ID will start with P and if you have the "Locacion de servicio" attribute will start with L. But both need to have correlative numbers. if i have P1 when i add a new certificate for a employee with "locacion de servicio" the custom id have to be "L1" and the next with P "P2"

how can i do this??

This is part of my employee model

CONTRACT_TYPES = (
    (1, ("Planta Permanente")),
    (2, ("Planta Contratada")),
    (3, ("Locación de servicio")),
)

class Employee(models.Model):

    cuil = models.CharField(
            unique=True,
            max_length=11,
            verbose_name=_('CUIL'),
        )
    name = models.CharField(
            max_length=50,
            verbose_name=_('first name'),
        )
    middle_name = models.CharField(
            blank=True,
            max_length=50,
            verbose_name=_('middle name'),
        )
    last_name = models.CharField(
            max_length=100,
            verbose_name=_('last name'),
        )
    have_children = models.BooleanField(
            default=False)
    contract = models.IntegerField(
            choices=CONTRACT_TYPES,
            verbose_name=_('contract'),
        )

And this is part of my Certificate model

class Certificate(models.Model):
    employee = models.ForeignKey(
            Employee,
            verbose_name=_('employee'),
        )
    place = models.IntegerField(
            choices=ACCIDENT_PLACE,
            default=1,
            verbose_name=_('place')
        )
    detail = models.CharField(
            max_length=255,
            verbose_name=_('detail'),
        )
    clinic = models.ForeignKey(
            Clinic,
            verbose_name=_('clinic'),
        )

Well thats what i need to do, if the employee have the contract type 1 or 2 identify with P1 P2 P3...P100...Pn and if is 3 the same but with L letter.

Any idea?

(Thanks very much)

Sayse
  • 42,633
  • 14
  • 77
  • 146
marcosgue
  • 627
  • 3
  • 8
  • 24
  • which model is it that you want this field added to? Is the numeric part sequential (increases 1 by 1) – e4c5 Aug 01 '16 at 12:22

1 Answers1

0

Have you read this approach in the docs: https://docs.djangoproject.com/en/1.9/ref/models/instances/#explicitly-specifying-auto-primary-key-values ?

Would that help if you manually specified your custom ID (a simple 'if' would do?) to have the P1,L1 or P2 in front and save as shown in the django docs link above? To make sure the numbers are consecuitive (I assume you want to have something like: L101, P102, P103, P204 etc.?) you could combine your custom ID with the auto-primary key, right?

So eventually your model will have the auto-generated ID and additionally your custom ID where the first two letters are specified as you wich and the following digits are a copy from the auto-generated primary-key.

Hope that helps!

user1544500
  • 2,067
  • 5
  • 25
  • 35
  • I'm going to read that when i get home bue the ids have to be consecutive but for each letter i'm going to have L101 L102 L103 and P101 P102 P103 too. Thanks for your answer – marcosgue Aug 02 '16 at 00:11