zoukankan      html  css  js  c++  java
  • 关于Django orm models文件定义函数的写法

    models.py

    
    


    class Candidate(BaseModel):
    name = models.CharField(max_length=64,
    verbose_name="候选人姓名",
    null=True,
    blank=True)
    email = models.EmailField(null=True,
    blank=True,
    verbose_name="候选人邮箱")
    code = models.CharField(max_length=48,
    verbose_name="每张表数据对应的uuid",
    null=True,
    blank=True)
    expire_time = models.DateTimeField(null=True,
    verbose_name='过期时间')

    class Meta:
    verbose_name = "候选人表"
    verbose_name_plural = verbose_name

    def __str__(self):
    return self.name


    class
    InvitationJobInterviewForm(BaseModel): invite_people = models.ForeignKey(to='Employees', verbose_name='邀请人', null=True, blank=True, related_name='invite_people_in_employees', on_delete=models.CASCADE) invited_people = models.ForeignKey(to='Candidate', verbose_name='被邀请人', null=True, blank=True, related_name='invited_people_in_candidate', on_delete=models.CASCADE) invite_people_code = models.CharField(max_length=64, verbose_name="邀请人code", null=True, blank=True) jobs = models.ForeignKey(to='Jobs', verbose_name="招聘职位", null=True, blank=True, on_delete=models.CASCADE, related_name='invite_people_jobs') class Meta: verbose_name = '邀请职位面试表' verbose_name_plural = verbose_name @property def get_invite_people_name(self): try: self.invite_people.name except Exception: return 'null' return self.invite_people.name @property def get_invited_people_name(self): try: self.invited_people.name except Exception: return 'null' return self.invited_people.name @property def get_job_name(self): try: self.jobs.jobname except Exception: return 'null' return self.jobs.jobname def __str__(self): return self.invite_people_code

    以候选人表举例,

        def get_invited_people_name(self):
            try:
                self.invited_people.name
            except Exception:
                return 'null'
            return self.invited_people.name

    self.invited_people 跨到外键关联的表中 self.invited_people.name跨到另外一张表中获取到name (这是正向查询)
    如果外键在另一张表中,也可以直接点另外一张表中的外键,这样可以在本张表跨到关联的那张表,再点name 即可获得到另一张表里面的name(反向查询)
  • 相关阅读:
    算法总结之 自然数组的排序
    算法总结之 计算数组最小和
    算法总结之 未排序数组中累加和小于或等于给定值的最长子数组长度
    算法总结之 未排序数组中累加和为给定值的最长子数组系列问题
    wget镜像网站并且下载到指定目录 2012-06-20 19:40:56
    用wget做站点镜像
    【亲测好用!】shell批量采集百度下拉框关键词
    网站发的文章有收录 但是没有排名怎么处理
    NGINX + LUA实现复杂的控制
    Linux下php安装Redis扩展
  • 原文地址:https://www.cnblogs.com/tangda/p/12229355.html
Copyright © 2011-2022 走看看