使用另一个模型添加模型约束



我目前正在开发一个带有候选人、应用程序和工作模型的应用程序跟踪应用程序。

理想情况下,我希望候选人只申请一次工作,但如果候选人愿意,他们应该能够申请另一份工作。

我遇到的问题是,由于应用程序模型位于候选人和作业之间,我不确定在哪里添加约束 - 应用程序模型通过应用程序模型中定义的关系"检测"候选人是否已存在于作业中。

添加唯一约束将只允许候选人在整个过程中应用一次,因此这并不理想。

候选人 A ->
  • 应用程序 A -> 作业 A # 应该有效
  • 候选 A ->
  • 应用程序 A -> 作业 A # 应抛出错误
  • 候选人 A -> 应用程序 B -> 作业 B # 应该工作

这是我的 models.py


class Candidate(models.Model):
# Candidate Personal and Contact Information
date_created = models.DateTimeField(auto_now_add=True)
first_name = models.CharField(max_length=120)
last_name = models.CharField(max_length=120)
email = models.EmailField()
phone = models.CharField(max_length=30)
# Candidate Location Information
city = models.CharField(max_length=120)
state = models.CharField(max_length=120)
country = CountryField(blank_label='Select Country')
zip_code = models.CharField(max_length=10)
def __str__(self):
return f'{self.first_name} {self.last_name}'

class Application(models.Model):
# Application Status Choices 
class ApplicationStatus(models.TextChoices):
...

# Application Stages Choices 
class ApplicationStage(models.TextChoices):
...

# Job-related Information
date_applied = models.DateTimeField(auto_now_add=True)
job = models.ForeignKey('Job', on_delete=models.CASCADE)
application_status = models.CharField(max_length=120,
choices=ApplicationStatus.choices,
default=ApplicationStatus.ACTIVE)
stage = models.CharField(max_length=120,
choices=ApplicationStage.choices,
default=ApplicationStage.APPLICATION)
# Applicant Information
candidate = models.ForeignKey(Candidate, 
related_name='applications', 
on_delete=models.CASCADE)
resume = models.FileField()
def __str__(self):
return str(self.job)

class Job(models.Model):
# Job Status Choices
class JobStatus(models.TextChoices):
...

# Employment Type Choices
class EmploymentType(models.TextChoices):
...
date_created = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(User, models.SET_NULL, blank=True, null=True)
job_status = models.CharField(max_length=120,
choices=JobStatus.choices,
default=JobStatus.DRAFT)
employment_type = models.CharField(max_length=120,
choices=EmploymentType.choices,
default=EmploymentType.FULL_TIME)
compensation_min = models.DecimalField(max_digits=6, decimal_places=2)
compensation_max = models.DecimalField(max_digits=6, decimal_places=2)
title = models.CharField(max_length=120)
description = models.TextField()
def __str__(self):
return self.title

这是我的 serializers.py

from rest_framework import serializers
from rct.models import (Candidate,
Application,
InterviewSchedule,
Scorecard,
Job)

class CandidateSerializer(serializers.ModelSerializer):
applications = serializers.StringRelatedField(many=True, read_only=True)
class Meta: 
model = Candidate
fields = '__all__'

class ApplicationSerializer(serializers.ModelSerializer):
class Meta: 
model = Application
fields = '__all__'

class JobSerializer(serializers.ModelSerializer):
class Meta: 
model = Job
fields = '__all__'

如果有人能指出我正确的方向,我将不胜感激。谢谢。

您可以在模型Applicationjobcandidate字段上使用索引unique_together索引。要获取更多信息,请访问官方模型选项文档。

最新更新