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

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

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

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

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

  • 候选人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__'

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

评论
樱小桃~
樱小桃~

You can use unique_together index on job and candidate fields in Application model Meta class. To get more information, please visit official model options doc.

点赞
评论