码迷,mamicode.com
首页 > 其他好文 > 详细

关于Django orm models文件定义函数的写法

时间:2020-01-22 20:11:26      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:can   inter   delete   related   form   面试   招聘   name   prope   

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(反向查询)

关于Django orm models文件定义函数的写法

标签:can   inter   delete   related   form   面试   招聘   name   prope   

原文地址:https://www.cnblogs.com/tangda/p/12229355.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!