Django-如何添加自定义ID列以标识我的对象? [英] Django - How to add a custom ID column to identify my objects?

查看:65
本文介绍了Django-如何添加自定义ID列以标识我的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(首先对不起我的英语不好,我希望你能理解我)

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

好吧,我必须向一个我的对象添加自定义ID型号,如果相关对象具有一个或其他属性,则此自定义ID将具有字母P或L。
具体来说,如果员工具有 Planta Permantente或 Planta Contratada属性,则自定义ID将以P开头,并且如果您具有 Locacion de servicio属性将以L开头。但是两者都需要具有相关数字。如果我为具有 locacion de servicio的员工添加新证书时拥有P1,则自定义ID必须为 L1,下一个为P P2

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"

我该怎么做?

这是我的员工模型的一部分

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'),
        )

这就是我需要做的,如果员工有合同类型1或2标识为P1 P2 P3 ... P100 ... Pn,如果3相同,则标识为L字母。

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.

有什么想法吗?

(非常感谢)

推荐答案

您是否已在docs: https:// docs.djangoproject.com/en/1.9/ref/models/instances/#explicitly-specifying-auto-primary-key-values 吗?

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

如果您手动指定了您的自定义ID(一个简单的'if'可以吗?)来帮助您将P1,L1或P2放在前面并保存,如上面的django docs链接所示,是否有帮助?为了确保数字是连续的(我假设您想使用L101,P102,P103,P204等?),可以将自定义ID与自动主键结合使用,对吗?

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?

因此,最终,您的模型将具有自动生成的ID和自定义ID,其中,前两个字母是您指定的,随后的数字是自动生成的主键。

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.

希望有帮助!

这篇关于Django-如何添加自定义ID列以标识我的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆