Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

Django - How to add a custom ID column to identify my objects?

(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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...